home *** CD-ROM | disk | FTP | other *** search
/ Languguage OS 2 / Languguage OS II Version 10-94 (Knowledge Media)(1994).ISO / gnu / gpp-1_42.lha / g++-1.42.0 / ld.c < prev    next >
C/C++ Source or Header  |  1991-10-19  |  131KB  |  4,769 lines

  1. /* Linker `ld' for GNU
  2.    Copyright (C) 1988 Free Software Foundation, Inc.
  3.  
  4.    This program is free software; you can redistribute it and/or modify
  5.    it under the terms of the GNU General Public License as published by
  6.    the Free Software Foundation; either version 1, or (at your option)
  7.    any later version.
  8.  
  9.    This program is distributed in the hope that it will be useful,
  10.    but WITHOUT ANY WARRANTY; without even the implied warranty of
  11.    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  12.    GNU General Public License for more details.
  13.  
  14.    You should have received a copy of the GNU General Public License
  15.    along with this program; if not, write to the Free Software
  16.    Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.  */
  17.  
  18. /* Written by Richard Stallman with some help from Eric Albert.
  19.    Set, indirect, and warning symbol features added by Randy Smith.  */
  20.    
  21. /* Define how to initialize system-dependent header fields.  */
  22. #ifdef sun
  23. /* Use Sun's TARGET convention.  */
  24. #ifndef TARGET
  25. #define SUN2 2
  26. #define SUN3 3
  27. #define SUN4 4
  28. #if defined(sparc)
  29. #define TARGET SUN4
  30. #else
  31. #if defined(mc68020) || defined(m68020)
  32. #define TARGET SUN3
  33. #else
  34. #define TARGET SUN2
  35. #endif
  36. #endif
  37. #else
  38. #define _CROSS_TARGET_ARCH TARGET  /* locate the correct a.out.h file */
  39. #endif
  40. #endif
  41.  
  42. #include <ar.h>
  43. #include <stdio.h>
  44. #include <sys/types.h>
  45. #include <sys/stat.h>
  46. #include <sys/file.h>
  47. #include <sys/time.h>
  48. #include <sys/resource.h>
  49. #ifndef sony_news
  50. #include <fcntl.h>
  51. #endif
  52.  
  53. #ifdef COFF_ENCAPSULATE
  54. #include "a.out.encap.h"
  55. #else
  56. #include <a.out.h>
  57. #endif
  58.  
  59. #ifndef N_SET_MAGIC
  60. #define N_SET_MAGIC(exec, val)  ((exec).a_magic = val)
  61. #endif
  62.  
  63. /* If compiled with GNU C, use the built-in alloca */
  64. #ifdef __GNUC__
  65. #define alloca __builtin_alloca
  66. #endif
  67.  
  68. /* Always use the GNU version of debugging symbol type codes, if possible.  */
  69.  
  70. #include "stab.h"
  71. #define CORE_ADDR unsigned long    /* For symseg.h */
  72. #include "symseg.h"
  73.  
  74. #ifdef USG
  75. #include <string.h>
  76. #else
  77. #include <strings.h>
  78. #endif
  79.  
  80. /* Determine whether we should attempt to handle (minimally)
  81.    N_BINCL and N_EINCL.  */
  82.  
  83. #if defined (__GNU_STAB__) || defined (N_BINCL)
  84. #define HAVE_SUN_STABS
  85. #endif
  86.  
  87. #define min(a,b) ((a) < (b) ? (a) : (b))
  88.  
  89. /* Macro to control the number of undefined references printed */
  90. #define MAX_UREFS_PRINTED    10
  91.  
  92. /* Size of a page; obtained from the operating system.  */
  93.  
  94. int page_size;
  95.  
  96. /* Name this program was invoked by.  */
  97.  
  98. char *progname;
  99.  
  100. /* System dependencies */
  101.  
  102. /* Define this if names etext, edata and end should not start with `_'.  */
  103. /* #define nounderscore 1 */
  104.  
  105. /* Define NON_NATIVE if using BSD or pseudo-BSD file format on a system
  106.    whose native format is different.  */
  107. /* #define NON_NATIVE */
  108.  
  109. /* Define this to specify the default executable format.  */
  110.  
  111. #ifdef hpux
  112. #define DEFAULT_MAGIC NMAGIC  /* hpux bugs screw ZMAGIC */
  113. #endif
  114.  
  115. #ifndef DEFAULT_MAGIC
  116. #define DEFAULT_MAGIC ZMAGIC
  117. #endif
  118.  
  119. /* Ordinary 4.3bsd lacks these macros in a.out.h.  */
  120.  
  121. #ifndef N_TXTADDR
  122. #if defined(vax) || defined(sony_news) || defined(hp300) || defined(pyr)
  123. #define N_TXTADDR(X) 0
  124. #endif
  125. #ifdef is68k
  126. #define N_TXTADDR(x)  (sizeof (struct exec))
  127. #endif
  128. #ifdef sequent
  129. #define    N_TXTADDR(x) (N_ADDRADJ(x))
  130. #endif
  131. #endif
  132.  
  133. #ifndef N_DATADDR
  134. #if defined(vax) || defined(sony_news) || defined(hp300) || defined(pyr)
  135. #define N_DATADDR(x) \
  136.     (((x).a_magic==OMAGIC)? (N_TXTADDR(x)+(x).a_text) \
  137.     : (page_size+((N_TXTADDR(x)+(x).a_text-1) & ~(page_size-1))))
  138. #endif
  139. #ifdef is68k
  140. #define SEGMENT_SIZE 0x20000
  141. #define N_DATADDR(x) \
  142. (((x).a_magic==Omagic)? (N_TXTADDR(x)+(x).a_text) \
  143.  : (SEGMENT_SIZE + ((N_TXTADDR(x)+(x).a_text-1) & ~(SEGMENT_SIZE-1))))
  144. #endif
  145. #ifdef sequent
  146. #define N_DATADDR(x) \
  147.     (((x).a_magic==OMAGIC)? (N_TXTADDR(x)+(x).a_text) \
  148.     : (page_size+(((x).a_text-1) & ~(page_size-1))))
  149. #endif
  150. #endif
  151.  
  152. #ifdef sun
  153. #if TARGET == SUN4
  154. #define INITIALIZE_HEADER \
  155. {outheader.a_machtype = M_SPARC; outheader.a_toolversion = 1;}
  156. #endif
  157. #if TARGET == SUN2
  158. #define INITIALIZE_HEADER outheader.a_machtype = M_68010
  159. #endif
  160. #ifndef INITIALIZE_HEADER
  161. #define INITIALIZE_HEADER outheader.a_machtype = M_68020
  162. #endif
  163. #define TEXT_START(x) N_PAGSIZ(x)
  164. #endif
  165. #ifdef ALTOS
  166. #define INITIALIZE_HEADER N_SET_MACHTYPE (outheader, M_68020)
  167. #endif
  168. #ifdef is68k
  169. #ifdef M_68020
  170. /* ISI rel 4.0D doesn't use it, and rel 3.05 doesn't have an
  171.    a_machtype field and so won't recognize the magic number.  To keep
  172.    binary compatibility for now, just ignore it */
  173. #define INITIALIZE_HEADER outheader.a_machtype = 0;
  174. #endif
  175. #endif
  176. #ifdef hpux
  177. #define INITIALIZE_HEADER N_SET_MACHTYPE (outheader, HP9000S200_ID)
  178. #endif
  179. #if defined(i386) && !defined(sequent)
  180. #define INITIALIZE_HEADER N_SET_MACHTYPE (outheader, M_386)
  181. #endif
  182.  
  183. #ifdef is68k
  184. /* This enables code to take care of an ugly hack in the ISI OS.
  185.    If a symbol beings with _$, then the object file is included only
  186.    if the rest of the symbol name has been referenced. */
  187. #define DOLLAR_KLUDGE
  188. #endif
  189.  
  190. /*
  191.  * Alloca include.
  192.  */
  193. #if defined(sun) && defined(sparc) && !defined(__GNUC__)
  194. #include "alloca.h"
  195. #endif
  196.  
  197. #ifndef L_SET
  198. #define L_SET 0
  199. #endif
  200.  
  201. /*
  202.  * Ok.  Following are the relocation information macros.  If your
  203.  * system should not be able to use the default set (below), you must
  204.  * define the following:
  205.  
  206.  *   relocation_info: This must be typedef'd (or #define'd) to the type
  207.  * of structure that is stored in the relocation info section of your
  208.  * a.out files.  Often this is defined in the a.out.h for your system.
  209.  *
  210.  *   RELOC_ADDRESS (rval): Offset into the current section of the
  211.  * <whatever> to be relocated.  *Must be an lvalue*.
  212.  *
  213.  *   RELOC_EXTERN_P (rval):  Is this relocation entry based on an
  214.  * external symbol (1), or was it fully resolved upon entering the
  215.  * loader (0) in which case some combination of the value in memory
  216.  * (if RELOC_MEMORY_ADD_P) and the extra (if RELOC_ADD_EXTRA) contains
  217.  * what the value of the relocation actually was.  *Must be an lvalue*.
  218.  *
  219.  *   RELOC_TYPE (rval): If this entry was fully resolved upon
  220.  * entering the loader, what type should it be relocated as?
  221.  *
  222.  *   RELOC_SYMBOL (rval): If this entry was not fully resolved upon
  223.  * entering the loader, what is the index of it's symbol in the symbol
  224.  * table?  *Must be a lvalue*.
  225.  *
  226.  *   RELOC_MEMORY_ADD_P (rval): This should return true if the final
  227.  * relocation value output here should be added to memory, or if the
  228.  * section of memory described should simply be set to the relocation
  229.  * value.
  230.  *
  231.  *   RELOC_ADD_EXTRA (rval): (Optional) This macro, if defined, gives
  232.  * an extra value to be added to the relocation value based on the
  233.  * individual relocation entry.  *Must be an lvalue if defined*.
  234.  *
  235.  *   RELOC_PCREL_P (rval): True if the relocation value described is
  236.  * pc relative.
  237.  *
  238.  *   RELOC_VALUE_RIGHTSHIFT (rval): Number of bits right to shift the
  239.  * final relocation value before putting it where it belongs.
  240.  *
  241.  *   RELOC_TARGET_SIZE (rval): log to the base 2 of the number of
  242.  * bytes of size this relocation entry describes; 1 byte == 0; 2 bytes
  243.  * == 1; 4 bytes == 2, and etc.  This is somewhat redundant (we could
  244.  * do everything in terms of the bit operators below), but having this
  245.  * macro could end up producing better code on machines without fancy
  246.  * bit twiddling.  Also, it's easier to understand/code big/little
  247.  * endian distinctions with this macro.
  248.  *
  249.  *   RELOC_TARGET_BITPOS (rval): The starting bit position within the
  250.  * object described in RELOC_TARGET_SIZE in which the relocation value
  251.  * will go.
  252.  *
  253.  *   RELOC_TARGET_BITSIZE (rval): How many bits are to be replaced
  254.  * with the bits of the relocation value.  It may be assumed by the
  255.  * code that the relocation value will fit into this many bits.  This
  256.  * may be larger than RELOC_TARGET_SIZE if such be useful.
  257.  *
  258.  *
  259.  *        Things I haven't implemented
  260.  *        ----------------------------
  261.  *
  262.  *    Values for RELOC_TARGET_SIZE other than 0, 1, or 2.
  263.  *
  264.  *    Pc relative relocation for External references.
  265.  *
  266.  *
  267.  */
  268.  
  269. /* The following #if has been modifed for cross compilation */
  270. /* It originally read:  #if defined(sun) && defined(sparc)  */
  271. /* Marc Ullman, Stanford University    Nov. 1 1989  */
  272. #if defined(sun) && (TARGET == SUN4)
  273. /* Sparc (Sun 4) macros */
  274. #undef relocation_info
  275. #define relocation_info                    reloc_info_sparc
  276. #define RELOC_ADDRESS(r)        ((r)->r_address)
  277. #define RELOC_EXTERN_P(r)               ((r)->r_extern)
  278. #define RELOC_TYPE(r)                   ((r)->r_index)
  279. #define RELOC_SYMBOL(r)                 ((r)->r_index)
  280. #define RELOC_MEMORY_SUB_P(r)        0
  281. #define RELOC_MEMORY_ADD_P(r)           0
  282. #define RELOC_ADD_EXTRA(r)              ((r)->r_addend)
  283. #define RELOC_PCREL_P(r)             \
  284.         ((r)->r_type >= RELOC_DISP8 && (r)->r_type <= RELOC_WDISP22)
  285. #define RELOC_VALUE_RIGHTSHIFT(r)       (reloc_target_rightshift[(r)->r_type])
  286. #define RELOC_TARGET_SIZE(r)            (reloc_target_size[(r)->r_type])
  287. #define RELOC_TARGET_BITPOS(r)          0
  288. #define RELOC_TARGET_BITSIZE(r)         (reloc_target_bitsize[(r)->r_type])
  289.  
  290. /* Note that these are very dependent on the order of the enums in
  291.    enum reloc_type (in a.out.h); if they change the following must be
  292.    changed */
  293. /* Also note that the last few may be incorrect; I have no information */
  294. static int reloc_target_rightshift[] = {
  295.   0, 0, 0, 0, 0, 0, 2, 2, 10, 0, 0, 0, 0, 0, 0,
  296. };
  297. static int reloc_target_size[] = {
  298.   0, 1, 2, 0, 1, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
  299. };
  300. static int reloc_target_bitsize[] = {
  301.   8, 16, 32, 8, 16, 32, 30, 22, 22, 22, 13, 10, 32, 32, 16,
  302. };
  303.  
  304. #define    MAX_ALIGNMENT    (sizeof (double))
  305. #endif
  306. #ifdef sequent
  307. #define RELOC_ADDRESS(r)        ((r)->r_address)
  308. #define RELOC_EXTERN_P(r)        ((r)->r_extern)
  309. #define RELOC_TYPE(r)        ((r)->r_symbolnum)
  310. #define RELOC_SYMBOL(r)        ((r)->r_symbolnum)
  311. #define RELOC_MEMORY_SUB_P(r)    ((r)->r_bsr)
  312. #define RELOC_MEMORY_ADD_P(r)    1
  313. #undef RELOC_ADD_EXTRA
  314. #define RELOC_PCREL_P(r)        ((r)->r_pcrel || (r)->r_bsr)
  315. #define RELOC_VALUE_RIGHTSHIFT(r)    0
  316. #define RELOC_TARGET_SIZE(r)        ((r)->r_length)
  317. #define RELOC_TARGET_BITPOS(r)    0
  318. #define RELOC_TARGET_BITSIZE(r)    32
  319. #endif
  320.  
  321. /* Default macros */
  322. #ifndef RELOC_ADDRESS
  323. #define RELOC_ADDRESS(r)        ((r)->r_address)
  324. #define RELOC_EXTERN_P(r)        ((r)->r_extern)
  325. #define RELOC_TYPE(r)        ((r)->r_symbolnum)
  326. #define RELOC_SYMBOL(r)        ((r)->r_symbolnum)
  327. #define RELOC_MEMORY_SUB_P(r)    0
  328. #define RELOC_MEMORY_ADD_P(r)    1
  329. #undef RELOC_ADD_EXTRA
  330. #define RELOC_PCREL_P(r)        ((r)->r_pcrel)
  331. #define RELOC_VALUE_RIGHTSHIFT(r)    0
  332. #define RELOC_TARGET_SIZE(r)        ((r)->r_length)
  333. #define RELOC_TARGET_BITPOS(r)    0
  334. #define RELOC_TARGET_BITSIZE(r)    32
  335. #endif
  336.  
  337. #ifndef MAX_ALIGNMENT
  338. #define    MAX_ALIGNMENT    (sizeof (int))
  339. #endif
  340.  
  341. #ifdef nounderscore
  342. #define LPREFIX '.'
  343. #else
  344. #define LPREFIX 'L'
  345. #endif
  346.  
  347. #ifndef TEXT_START
  348. #define TEXT_START(x) N_TXTADDR(x)
  349. #endif
  350.  
  351. /* Special global symbol types understood by GNU LD.  */
  352.  
  353. /* The following type indicates the definition of a symbol as being
  354.    an indirect reference to another symbol.  The other symbol
  355.    appears as an undefined reference, immediately following this symbol.
  356.  
  357.    Indirection is asymmetrical.  The other symbol's value will be used
  358.    to satisfy requests for the indirect symbol, but not vice versa.
  359.    If the other symbol does not have a definition, libraries will
  360.    be searched to find a definition.
  361.  
  362.    So, for example, the following two lines placed in an assembler
  363.    input file would result in an object file which would direct gnu ld
  364.    to resolve all references to symbol "foo" as references to symbol
  365.    "bar".
  366.  
  367.     .stabs "_foo",11,0,0,0
  368.     .stabs "_bar",1,0,0,0
  369.  
  370.    Note that (11 == (N_INDR | N_EXT)) and (1 == (N_UNDF | N_EXT)).  */
  371.  
  372. #ifndef N_INDR
  373. #define N_INDR 0xa
  374. #endif
  375.  
  376. /* The following symbols refer to set elements.  These are expected
  377.    only in input to the loader; they should not appear in loader
  378.    output (unless relocatable output is requested).  To be recognized
  379.    by the loader, the input symbols must have their N_EXT bit set.
  380.    All the N_SET[ATDB] symbols with the same name form one set.  The
  381.    loader collects all of these elements at load time and outputs a
  382.    vector for each name.
  383.    Space (an array of 32 bit words) is allocated for the set in the
  384.    data section, and the n_value field of each set element value is
  385.    stored into one word of the array.
  386.    The first word of the array is the length of the set (number of
  387.    elements).  The last word of the vector is set to zero for possible
  388.    use by incremental loaders.  The array is ordered by the linkage
  389.    order; the first symbols which the linker encounters will be first
  390.    in the array.
  391.  
  392.    In C syntax this looks like:
  393.  
  394.     struct set_vector {
  395.       unsigned int length;
  396.       unsigned int vector[length];
  397.       unsigned int always_zero;
  398.     };
  399.  
  400.    Before being placed into the array, each element is relocated
  401.    according to its type.  This allows the loader to create an array
  402.    of pointers to objects automatically.  N_SETA type symbols will not
  403.    be relocated.
  404.  
  405.    The address of the set is made into an N_SETV symbol
  406.    whose name is the same as the name of the set.
  407.    This symbol acts like a N_DATA global symbol
  408.    in that it can satisfy undefined external references.
  409.  
  410.    For the purposes of determining whether or not to load in a library
  411.    file, set element definitions are not considered "real
  412.    definitions"; they will not cause the loading of a library
  413.    member.
  414.  
  415.    If relocatable output is requested, none of this processing is
  416.    done.  The symbols are simply relocated and passed through to the
  417.    output file.
  418.  
  419.    So, for example, the following three lines of assembler code
  420.    (whether in one file or scattered between several different ones)
  421.    will produce a three element vector (total length is five words;
  422.    see above), referenced by the symbol "_xyzzy", which will have the
  423.    addresses of the routines _init1, _init2, and _init3.
  424.  
  425.    *NOTE*: If symbolic addresses are used in the n_value field of the
  426.    defining .stabs, those symbols must be defined in the same file as
  427.    that containing the .stabs.
  428.  
  429.     .stabs "_xyzzy",23,0,0,_init1
  430.     .stabs "_xyzzy",23,0,0,_init2
  431.     .stabs "_xyzzy",23,0,0,_init3
  432.  
  433.    Note that (23 == (N_SETT | N_EXT)).  */
  434.  
  435. #ifndef N_SETA
  436. #define    N_SETA    0x14        /* Absolute set element symbol */
  437. #endif                /* This is input to LD, in a .o file.  */
  438.  
  439. #ifndef N_SETT
  440. #define    N_SETT    0x16        /* Text set element symbol */
  441. #endif                /* This is input to LD, in a .o file.  */
  442.  
  443. #ifndef N_SETD
  444. #define    N_SETD    0x18        /* Data set element symbol */
  445. #endif                /* This is input to LD, in a .o file.  */
  446.  
  447. #ifndef N_SETB
  448. #define    N_SETB    0x1A        /* Bss set element symbol */
  449. #endif                /* This is input to LD, in a .o file.  */
  450.  
  451. /* Macros dealing with the set element symbols defined in a.out.h */
  452. #define    SET_ELEMENT_P(x)    ((x)>=N_SETA&&(x)<=(N_SETB|N_EXT))
  453. #define TYPE_OF_SET_ELEMENT(x)    ((x)-N_SETA+N_ABS)
  454.  
  455. #ifndef N_SETV
  456. #define N_SETV    0x1C        /* Pointer to set vector in data area.  */
  457. #endif                /* This is output from LD.  */
  458.  
  459. /* If a this type of symbol is encountered, its name is a warning
  460.    message to print each time the symbol referenced by the next symbol
  461.    table entry is referenced.
  462.  
  463.    This feature may be used to allow backwards compatibility with
  464.    certain functions (eg. gets) but to discourage programmers from
  465.    their use.
  466.  
  467.    So if, for example, you wanted to have ld print a warning whenever
  468.    the function "gets" was used in their C program, you would add the
  469.    following to the assembler file in which gets is defined:
  470.  
  471.     .stabs "Obsolete function \"gets\" referenced",30,0,0,0
  472.     .stabs "_gets",1,0,0,0
  473.  
  474.    These .stabs do not necessarily have to be in the same file as the
  475.    gets function, they simply must exist somewhere in the compilation.  */
  476.  
  477. #ifndef N_WARNING
  478. #define N_WARNING 0x1E        /* Warning message to print if symbol
  479.                    included */
  480. #endif                /* This is input to ld */
  481.  
  482. #ifndef __GNU_STAB__
  483.  
  484. /* Line number for the data section.  This is to be used to describe
  485.    the source location of a variable declaration.  */
  486. #ifndef N_DSLINE
  487. #define N_DSLINE (N_SLINE+N_DATA-N_TEXT)
  488. #endif
  489.  
  490. /* Line number for the bss section.  This is to be used to describe
  491.    the source location of a variable declaration.  */
  492. #ifndef N_BSLINE
  493. #define N_BSLINE (N_SLINE+N_BSS-N_TEXT)
  494. #endif
  495.  
  496. #endif /* not __GNU_STAB__ */
  497.  
  498. /* Symbol table */
  499.  
  500. /* Global symbol data is recorded in these structures,
  501.    one for each global symbol.
  502.    They are found via hashing in 'symtab', which points to a vector of buckets.
  503.    Each bucket is a chain of these structures through the link field.  */
  504.  
  505. typedef
  506.   struct glosym
  507.     {
  508.       /* Pointer to next symbol in this symbol's hash bucket.  */
  509.       struct glosym *link;
  510.       /* Name of this symbol.  */
  511.       char *name;
  512.       /* Value of this symbol as a global symbol.  */
  513.       long value;
  514.       /* Chain of external 'nlist's in files for this symbol, both defs
  515.      and refs.  */
  516.       struct nlist *refs;
  517.       /* Any warning message that might be associated with this symbol
  518.          from an N_WARNING symbol encountered. */
  519.       char *warning;
  520.       /* Nonzero means definitions of this symbol as common have been seen,
  521.      and the value here is the largest size specified by any of them.  */
  522.       int max_common_size;
  523.       /* For relocatable_output, records the index of this global sym in the
  524.      symbol table to be written, with the first global sym given index 0.*/
  525.       int def_count;
  526.       /* Nonzero means a definition of this global symbol is known to exist.
  527.      Library members should not be loaded on its account.  */
  528.       char defined;
  529.       /* Nonzero means a reference to this global symbol has been seen
  530.      in a file that is surely being loaded.
  531.      A value higher than 1 is the n_type code for the symbol's
  532.      definition.  */
  533.       char referenced;
  534.       /* A count of the number of undefined references printed for a
  535.      specific symbol.  If a symbol is unresolved at the end of
  536.      digest_symbols (and the loading run is supposed to produce
  537.      relocatable output) do_file_warnings keeps track of how many
  538.      unresolved reference error messages have been printed for
  539.      each symbol here.  When the number hits MAX_UREFS_PRINTED,
  540.      messages stop. */
  541.       unsigned char undef_refs;
  542.       /* 1 means that this symbol has multiple definitions.  2 means
  543.          that it has multiple definitions, and some of them are set
  544.      elements, one of which has been printed out already.  */
  545.       unsigned char multiply_defined;
  546.       /* Nonzero means print a message at all refs or defs of this symbol */
  547.       char trace;
  548.     }
  549.   symbol;
  550.  
  551. /* Demangler for C++. */
  552. extern char *cplus_demangle ();
  553.  
  554. /* Demangler function to use. */
  555. char *(*demangler)() = NULL;
  556.  
  557. /* Number of buckets in symbol hash table */
  558. #define    TABSIZE    1009
  559.  
  560. /* The symbol hash table: a vector of TABSIZE pointers to struct glosym. */
  561. symbol *symtab[TABSIZE];
  562.  
  563. /* Number of symbols in symbol hash table. */
  564. int num_hash_tab_syms = 0;
  565.  
  566. /* Count the number of nlist entries that are for local symbols.
  567.    This count and the three following counts
  568.    are incremented as as symbols are entered in the symbol table.  */
  569. int local_sym_count;
  570.  
  571. /* Count number of nlist entries that are for local symbols
  572.    whose names don't start with L. */
  573. int non_L_local_sym_count;
  574.  
  575. /* Count the number of nlist entries for debugger info.  */
  576. int debugger_sym_count;
  577.  
  578. /* Count the number of global symbols referenced and not defined.  */
  579. int undefined_global_sym_count;
  580.  
  581. /* Count the number of global symbols multiply defined.  */
  582. int multiple_def_count;
  583.  
  584. /* Count the number of defined global symbols.
  585.    Each symbol is counted only once
  586.    regardless of how many different nlist entries refer to it,
  587.    since the output file will need only one nlist entry for it.
  588.    This count is computed by `digest_symbols';
  589.    it is undefined while symbols are being loaded. */
  590. int defined_global_sym_count;
  591.  
  592. /* Count the number of symbols defined through common declarations.
  593.    This count is kept in symdef_library, linear_library, and
  594.    enter_global_ref.  It is incremented when the defined flag is set
  595.    in a symbol because of a common definition, and decremented when
  596.    the symbol is defined "for real" (ie. by something besides a common
  597.    definition).  */
  598. int common_defined_global_count;
  599.  
  600. /* Count the number of set element type symbols and the number of
  601.    separate vectors which these symbols will fit into.  See the
  602.    GNU a.out.h for more info.
  603.    This count is computed by 'enter_file_symbols' */
  604. int set_symbol_count;
  605. int set_vector_count;
  606.  
  607. /* Define a linked list of strings which define symbols which should
  608.    be treated as set elements even though they aren't.  Any symbol
  609.    with a prefix matching one of these should be treated as a set
  610.    element.
  611.  
  612.    This is to make up for deficiencies in many assemblers which aren't
  613.    willing to pass any stabs through to the loader which they don't
  614.    understand.  */
  615. struct string_list_element {
  616.   char *str;
  617.   struct string_list_element *next;
  618. };
  619.  
  620. struct string_list_element *set_element_prefixes;
  621.  
  622. /* Count the number of definitions done indirectly (ie. done relative
  623.    to the value of some other symbol. */
  624. int global_indirect_count;
  625.  
  626. /* Count the number of warning symbols encountered. */
  627. int warning_count;
  628.  
  629. /* Total number of symbols to be written in the output file.
  630.    Computed by digest_symbols from the variables above.  */
  631. int nsyms;
  632.  
  633.  
  634. /* Nonzero means ptr to symbol entry for symbol to use as start addr.
  635.    -e sets this.  */
  636. symbol *entry_symbol;
  637.  
  638. symbol *edata_symbol;   /* the symbol _edata */
  639. symbol *etext_symbol;   /* the symbol _etext */
  640. symbol *end_symbol;    /* the symbol _end */
  641.  
  642. /* Each input file, and each library member ("subfile") being loaded,
  643.    has a `file_entry' structure for it.
  644.  
  645.    For files specified by command args, these are contained in the vector
  646.    which `file_table' points to.
  647.  
  648.    For library members, they are dynamically allocated,
  649.    and chained through the `chain' field.
  650.    The chain is found in the `subfiles' field of the `file_entry'.
  651.    The `file_entry' objects for the members have `superfile' fields pointing
  652.    to the one for the library.  */
  653.  
  654. struct file_entry {
  655.   /* Name of this file.  */
  656.   char *filename;
  657.   /* Name to use for the symbol giving address of text start */
  658.   /* Usually the same as filename, but for a file spec'd with -l
  659.      this is the -l switch itself rather than the filename.  */
  660.   char *local_sym_name;
  661.  
  662.   /* Describe the layout of the contents of the file */
  663.  
  664.   /* The file's a.out header.  */
  665.   struct exec header;
  666.   /* Offset in file of GDB symbol segment, or 0 if there is none.  */
  667.   int symseg_offset;
  668.  
  669.   /* Describe data from the file loaded into core */
  670.  
  671.   /* Symbol table of the file.  */
  672.   struct nlist *symbols;
  673.   /* Size in bytes of string table.  */
  674.   int string_size;
  675.   /* Pointer to the string table.
  676.      The string table is not kept in core all the time,
  677.      but when it is in core, its address is here.  */
  678.   char *strings;
  679.  
  680.   /* Next two used only if `relocatable_output' or if needed for */
  681.   /* output of undefined reference line numbers. */
  682.  
  683.   /* Text reloc info saved by `write_text' for `coptxtrel'.  */
  684.   struct relocation_info *textrel;
  685.   /* Data reloc info saved by `write_data' for `copdatrel'.  */
  686.   struct relocation_info *datarel;
  687.  
  688.   /* Relation of this file's segments to the output file */
  689.  
  690.   /* Start of this file's text seg in the output file core image.  */
  691.   int text_start_address;
  692.   /* Start of this file's data seg in the output file core image.  */
  693.   int data_start_address;
  694.   /* Start of this file's bss seg in the output file core image.  */
  695.   int bss_start_address;
  696.   /* Offset in bytes in the output file symbol table
  697.      of the first local symbol for this file.  Set by `write_file_symbols'.  */
  698.   int local_syms_offset;
  699.  
  700.   /* For library members only */
  701.  
  702.   /* For a library, points to chain of entries for the library members.  */
  703.   struct file_entry *subfiles;
  704.   /* For a library member, offset of the member within the archive.
  705.      Zero for files that are not library members.  */
  706.   int starting_offset;
  707.   /* Size of contents of this file, if library member.  */
  708.   int total_size;
  709.   /* For library member, points to the library's own entry.  */
  710.   struct file_entry *superfile;
  711.   /* For library member, points to next entry for next member.  */
  712.   struct file_entry *chain;
  713.  
  714.   /* 1 if file is a library. */
  715.   char library_flag;
  716.  
  717.   /* 1 if file's header has been read into this structure.  */
  718.   char header_read_flag;
  719.  
  720.   /* 1 means search a set of directories for this file.  */
  721.   char search_dirs_flag;
  722.  
  723.   /* 1 means this is base file of incremental load.
  724.      Do not load this file's text or data.
  725.      Also default text_start to after this file's bss. */
  726.   char just_syms_flag;
  727. };
  728.  
  729. /* Vector of entries for input files specified by arguments.
  730.    These are all the input files except for members of specified libraries.  */
  731. struct file_entry *file_table;
  732.  
  733. /* Length of that vector.  */
  734. int number_of_files;
  735.  
  736. /* When loading the text and data, we can avoid doing a close
  737.    and another open between members of the same library.
  738.  
  739.    These two variables remember the file that is currently open.
  740.    Both are zero if no file is open.
  741.  
  742.    See `each_file' and `file_close'.  */
  743.  
  744. struct file_entry *input_file;
  745. int input_desc;
  746.  
  747. /* The name of the file to write; "a.out" by default.  */
  748.  
  749. char *output_filename;
  750.  
  751. /* Descriptor for writing that file with `mywrite'.  */
  752.  
  753. int outdesc;
  754.  
  755. /* Header for that file (filled in by `write_header').  */
  756.  
  757. struct exec outheader;
  758.  
  759. #ifdef COFF_ENCAPSULATE
  760. struct coffheader coffheader;
  761. int need_coff_header;
  762. #endif
  763.  
  764. /* The following are computed by `digest_symbols'.  */
  765.  
  766. int text_size;        /* total size of text of all input files.  */
  767. int data_size;        /* total size of data of all input files.  */
  768. int bss_size;        /* total size of bss of all input files.  */
  769. int text_reloc_size;    /* total size of text relocation of all input files.  */
  770. int data_reloc_size;    /* total size of data relocation of all input */
  771.             /* files.  */
  772.  
  773. /* Specifications of start and length of the area reserved at the end
  774.    of the text segment for the set vectors.  Computed in 'digest_symbols' */
  775. int set_sect_start;
  776. int set_sect_size;
  777.  
  778. /* Pointer for in core storage for the above vectors, before they are
  779.    written. */
  780. unsigned long *set_vectors;
  781.  
  782. /* Amount of cleared space to leave between the text and data segments.  */
  783.  
  784. int text_pad;
  785.  
  786. /* Amount of bss segment to include as part of the data segment.  */
  787.  
  788. int data_pad;
  789.  
  790. /* Format of __.SYMDEF:
  791.    First, a longword containing the size of the 'symdef' data that follows.
  792.    Second, zero or more 'symdef' structures.
  793.    Third, a longword containing the length of symbol name strings.
  794.    Fourth, zero or more symbol name strings (each followed by a null).  */
  795.  
  796. struct symdef {
  797.   int symbol_name_string_index;
  798.   int library_member_offset;
  799. };
  800.  
  801. /* Record most of the command options.  */
  802.  
  803. /* Address we assume the text section will be loaded at.
  804.    We relocate symbols and text and data for this, but we do not
  805.    write any padding in the output file for it.  */
  806. int text_start;
  807.  
  808. /* Offset of default entry-pc within the text section.  */
  809. int entry_offset;
  810.  
  811. /* Address we decide the data section will be loaded at.  */
  812. int data_start;
  813.  
  814. /* `text-start' address is normally this much plus a page boundary.
  815.    This is not a user option; it is fixed for each system.  */
  816. int text_start_alignment;
  817.  
  818. /* Nonzero if -T was specified in the command line.
  819.    This prevents text_start from being set later to default values.  */
  820. int T_flag_specified;
  821.  
  822. /* Nonzero if -Tdata was specified in the command line.
  823.    This prevents data_start from being set later to default values.  */
  824. int Tdata_flag_specified;
  825.  
  826. /* Size to pad data section up to.
  827.    We simply increase the size of the data section, padding with zeros,
  828.    and reduce the size of the bss section to match.  */
  829. int specified_data_size;
  830.  
  831. /* Magic number to use for the output file, set by switch.  */
  832. int magic;
  833.  
  834. /* Nonzero means print names of input files as processed.  */
  835. int trace_files;
  836.  
  837. /* Which symbols should be stripped (omitted from the output):
  838.    none, all, or debugger symbols.  */
  839. enum { STRIP_NONE, STRIP_ALL, STRIP_DEBUGGER } strip_symbols;
  840.  
  841. /* Which local symbols should be omitted:
  842.    none, all, or those starting with L.
  843.    This is irrelevant if STRIP_NONE.  */
  844. enum { DISCARD_NONE, DISCARD_ALL, DISCARD_L } discard_locals;
  845.  
  846. /* 1 => write load map.  */
  847. int write_map;
  848.  
  849. /* 1 => write relocation into output file so can re-input it later.  */
  850. int relocatable_output;
  851.  
  852. /* 1 => assign space to common symbols even if `relocatable_output'.  */
  853. int force_common_definition;
  854.  
  855. /* Standard directories to search for files specified by -l.  */
  856. char *standard_search_dirs[] =
  857. #ifdef STANDARD_SEARCH_DIRS
  858.   {STANDARD_SEARCH_DIRS};
  859. #else
  860. #ifdef NON_NATIVE
  861.   {"/usr/local/lib/gnu"};
  862. #else
  863.   {"/lib", "/usr/lib", "/usr/local/lib"};
  864. #endif
  865. #endif
  866.  
  867. /* Actual vector of directories to search;
  868.    this contains those specified with -L plus the standard ones.  */
  869. char **search_dirs;
  870.  
  871. /* Length of the vector `search_dirs'.  */
  872. int n_search_dirs;
  873.  
  874. /* Non zero means to create the output executable. */
  875. /* Cleared by nonfatal errors.  */
  876. int make_executable;
  877.  
  878. /* Force the executable to be output, even if there are non-fatal
  879.    errors */
  880. int force_executable;
  881.  
  882. /* Keep a list of any symbols referenced from the command line (so
  883.    that error messages for these guys can be generated). This list is
  884.    zero terminated. */
  885. struct glosym **cmdline_references;
  886. int cl_refs_allocated;
  887.  
  888. void bcopy (), bzero ();
  889. int malloc (), realloc ();
  890. #ifndef alloca
  891. int alloca ();
  892. #endif
  893. int free ();
  894.  
  895. int xmalloc ();
  896. int xrealloc ();
  897. void fatal ();
  898. void fatal_with_file ();
  899. void perror_name ();
  900. void perror_file ();
  901. void error ();
  902.  
  903. void digest_symbols ();
  904. void print_symbols ();
  905. void load_symbols ();
  906. void decode_command ();
  907. void list_undefined_symbols ();
  908. void list_unresolved_references ();
  909. void write_output ();
  910. void write_header ();
  911. void write_text ();
  912. void read_file_relocation ();
  913. void write_data ();
  914. void write_rel ();
  915. void write_syms ();
  916. void write_symsegs ();
  917. void mywrite ();
  918. void symtab_init ();
  919. void padfile ();
  920. char *concat ();
  921. char *get_file_name ();
  922. symbol *getsym (), *getsym_soft ();
  923.  
  924. int
  925. main (argc, argv)
  926.      char **argv;
  927.      int argc;
  928. {
  929. /*   Added this to stop ld core-dumping on very large .o files.    */
  930. #ifdef RLIMIT_STACK
  931.   /* Get rid of any avoidable limit on stack size.  */
  932.   {
  933.     struct rlimit rlim;
  934.  
  935.     /* Set the stack limit huge so that alloca does not fail. */
  936.     getrlimit (RLIMIT_STACK, &rlim);
  937.     rlim.rlim_cur = rlim.rlim_max;
  938.     setrlimit (RLIMIT_STACK, &rlim);
  939.   }
  940. #endif /* RLIMIT_STACK */
  941.  
  942.   page_size = getpagesize ();
  943.   progname = argv[0];
  944.  
  945.   /* Clear the cumulative info on the output file.  */
  946.  
  947.   text_size = 0;
  948.   data_size = 0;
  949.   bss_size = 0;
  950.   text_reloc_size = 0;
  951.   data_reloc_size = 0;
  952.  
  953.   data_pad = 0;
  954.   text_pad = 0;
  955.  
  956.   /* Initialize the data about options.  */
  957.  
  958.   specified_data_size = 0;
  959.   strip_symbols = STRIP_NONE;
  960.   trace_files = 0;
  961.   discard_locals = DISCARD_NONE;
  962.   entry_symbol = 0;
  963.   write_map = 0;
  964.   relocatable_output = 0;
  965.   force_common_definition = 0;
  966.   T_flag_specified = 0;
  967.   Tdata_flag_specified = 0;
  968.   magic = DEFAULT_MAGIC;
  969.   make_executable = 1;
  970.   force_executable = 0;
  971.   set_element_prefixes = 0;
  972.  
  973.   /* Initialize the cumulative counts of symbols.  */
  974.  
  975.   local_sym_count = 0;
  976.   non_L_local_sym_count = 0;
  977.   debugger_sym_count = 0;
  978.   undefined_global_sym_count = 0;
  979.   set_symbol_count = 0;
  980.   set_vector_count = 0;
  981.   global_indirect_count = 0;
  982.   warning_count = 0;
  983.   multiple_def_count = 0;
  984.   common_defined_global_count = 0;
  985.  
  986.   /* Keep a list of symbols referenced from the command line */
  987.   cl_refs_allocated = 10;
  988.   cmdline_references
  989.     = (struct glosym **) xmalloc (cl_refs_allocated
  990.                   * sizeof(struct glosym *));
  991.   *cmdline_references = 0;
  992.  
  993.   /* Completely decode ARGV.  */
  994.  
  995.   decode_command (argc, argv);
  996.  
  997.   /* Create the symbols `etext', `edata' and `end'.  */
  998.  
  999.   if (!relocatable_output)
  1000.     symtab_init ();
  1001.  
  1002.   /* Determine whether to count the header as part of
  1003.      the text size, and initialize the text size accordingly.
  1004.      This depends on the kind of system and on the output format selected.  */
  1005.  
  1006.   N_SET_MAGIC (outheader, magic);
  1007. #ifdef INITIALIZE_HEADER
  1008.   INITIALIZE_HEADER;
  1009. #endif
  1010.  
  1011.   text_size = sizeof (struct exec);
  1012. #ifdef COFF_ENCAPSULATE
  1013.   if (relocatable_output == 0 && file_table[0].just_syms_flag == 0)
  1014.     {
  1015.       need_coff_header = 1;
  1016.       /* set this flag now, since it will change the values of N_TXTOFF, etc */
  1017.       N_SET_FLAGS (outheader, N_FLAGS_COFF_ENCAPSULATE);
  1018.       text_size += sizeof (struct coffheader);
  1019.     }
  1020. #endif
  1021.  
  1022.   text_size -= N_TXTOFF (outheader);
  1023.  
  1024.   if (text_size < 0)
  1025.     text_size = 0;
  1026.   entry_offset = text_size;
  1027.  
  1028.   if (!T_flag_specified && !relocatable_output)
  1029.     text_start = TEXT_START (outheader);
  1030.  
  1031.   /* The text-start address is normally this far past a page boundary.  */
  1032.   text_start_alignment = text_start % page_size;
  1033.  
  1034.   /* Load symbols of all input files.
  1035.      Also search all libraries and decide which library members to load.  */
  1036.  
  1037.   load_symbols ();
  1038.  
  1039.   /* Compute where each file's sections go, and relocate symbols.  */
  1040.  
  1041.   digest_symbols ();
  1042.  
  1043.   /* Print error messages for any missing symbols, for any warning
  1044.      symbols, and possibly multiple definitions */
  1045.  
  1046.   do_warnings (stderr);
  1047.  
  1048.   /* Print a map, if requested.  */
  1049.  
  1050.   if (write_map) print_symbols (stdout);
  1051.  
  1052.   /* Write the output file.  */
  1053.  
  1054.   if (make_executable || force_executable)
  1055.     write_output ();
  1056.  
  1057.   exit (!make_executable);
  1058. }
  1059.  
  1060. void decode_option ();
  1061.  
  1062. /* Analyze a command line argument.
  1063.    Return 0 if the argument is a filename.
  1064.    Return 1 if the argument is a option complete in itself.
  1065.    Return 2 if the argument is a option which uses an argument.
  1066.  
  1067.    Thus, the value is the number of consecutive arguments
  1068.    that are part of options.  */
  1069.  
  1070. int
  1071. classify_arg (arg)
  1072.      register char *arg;
  1073. {
  1074.   if (*arg != '-') return 0;
  1075.   switch (arg[1])
  1076.     {
  1077.     case 'A':
  1078.     case 'D':
  1079.     case 'e':
  1080.     case 'L':
  1081.     case 'l':
  1082.     case 'o':
  1083.     case 'u':
  1084.     case 'V':
  1085.     case 'y':
  1086.       if (arg[2])
  1087.     return 1;
  1088.       return 2;
  1089.  
  1090.     case 'B':
  1091.       if (! strcmp (&arg[2], "static"))
  1092.     return 1;
  1093.  
  1094.     case 'T':
  1095.       if (arg[2] == 0)
  1096.     return 2;
  1097.       if (! strcmp (&arg[2], "text"))
  1098.     return 2;
  1099.       if (! strcmp (&arg[2], "data"))
  1100.     return 2;
  1101.       return 1;
  1102.     }
  1103.  
  1104.   return 1;
  1105. }
  1106.  
  1107. /* Process the command arguments,
  1108.    setting up file_table with an entry for each input file,
  1109.    and setting variables according to the options.  */
  1110.  
  1111. void
  1112. decode_command (argc, argv)
  1113.      char **argv;
  1114.      int argc;
  1115. {
  1116.   register int i;
  1117.   register struct file_entry *p;
  1118.  
  1119.   number_of_files = 0;
  1120.   output_filename = "a.out";
  1121.  
  1122.   n_search_dirs = 0;
  1123.   search_dirs = (char **) xmalloc (sizeof (char *));
  1124.  
  1125.   /* First compute number_of_files so we know how long to make file_table.  */
  1126.   /* Also process most options completely.  */
  1127.  
  1128.   for (i = 1; i < argc; i++)
  1129.     {
  1130.       register int code = classify_arg (argv[i]);
  1131.       if (code)
  1132.     {
  1133.       if (i + code > argc)
  1134.         fatal ("no argument following %s\n", argv[i]);
  1135.  
  1136.       decode_option (argv[i], argv[i+1]);
  1137.  
  1138.       if (argv[i][1] == 'l' || argv[i][1] == 'A')
  1139.         number_of_files++;
  1140.  
  1141.       i += code - 1;
  1142.     }
  1143.       else
  1144.     number_of_files++;
  1145.     }
  1146.  
  1147.   if (!number_of_files)
  1148.     fatal ("no input files", 0);
  1149.  
  1150.   p = file_table
  1151.     = (struct file_entry *) xmalloc (number_of_files * sizeof (struct file_entry));
  1152.   bzero (p, number_of_files * sizeof (struct file_entry));
  1153.  
  1154.   /* Now scan again and fill in file_table.  */
  1155.   /* All options except -A and -l are ignored here.  */
  1156.  
  1157.   for (i = 1; i < argc; i++)
  1158.     {
  1159.       register int code = classify_arg (argv[i]);
  1160.  
  1161.       if (code)
  1162.     {
  1163.       char *string;
  1164.       if (code == 2)
  1165.         string = argv[i+1];
  1166.       else
  1167.         string = &argv[i][2];
  1168.  
  1169.       if (argv[i][1] == 'A')
  1170.         {
  1171.           if (p != file_table)
  1172.         fatal ("-A specified before an input file other than the first");
  1173.  
  1174.           p->filename = string;
  1175.           p->local_sym_name = string;
  1176.           p->just_syms_flag = 1;
  1177.           p++;
  1178.         }
  1179.       if (argv[i][1] == 'l')
  1180.         {
  1181.           p->filename = concat ("lib", string, ".a");
  1182.           p->local_sym_name = concat ("-l", string, "");
  1183.           p->search_dirs_flag = 1;
  1184.           p++;
  1185.         }
  1186.       i += code - 1;
  1187.     }
  1188.       else
  1189.     {
  1190.       p->filename = argv[i];
  1191.       p->local_sym_name = argv[i];
  1192.       p++;
  1193.     }
  1194.     }
  1195.  
  1196.   /* Now check some option settings for consistency.  */
  1197.  
  1198. #ifdef NMAGIC
  1199.   if ((magic == ZMAGIC || magic == NMAGIC)
  1200. #else
  1201.   if ((magic == ZMAGIC)
  1202. #endif
  1203.       && (text_start - text_start_alignment) & (page_size - 1))
  1204.     fatal ("-T argument not multiple of page size, with sharable output", 0);
  1205.  
  1206.   /* Append the standard search directories to the user-specified ones.  */
  1207.   {
  1208.     int n = sizeof standard_search_dirs / sizeof standard_search_dirs[0];
  1209.     n_search_dirs += n;
  1210.     search_dirs
  1211.       = (char **) xrealloc (search_dirs, n_search_dirs * sizeof (char *));
  1212.     bcopy (standard_search_dirs, &search_dirs[n_search_dirs - n],
  1213.        n * sizeof (char *));
  1214.   }
  1215. }
  1216.  
  1217.  
  1218. void
  1219. add_cmdline_ref (sp)
  1220.      struct glosym *sp;
  1221. {
  1222.   struct glosym **ptr;
  1223.  
  1224.   for (ptr = cmdline_references;
  1225.        ptr < cmdline_references + cl_refs_allocated && *ptr;
  1226.        ptr++)
  1227.     ;
  1228.  
  1229.   if (ptr >= cmdline_references + cl_refs_allocated - 1)
  1230.     {
  1231.       int diff = ptr - cmdline_references;
  1232.  
  1233.       cl_refs_allocated *= 2;
  1234.       cmdline_references = (struct glosym **)
  1235.     xrealloc (cmdline_references,
  1236.          cl_refs_allocated * sizeof (struct glosym *));
  1237.       ptr = cmdline_references + diff;
  1238.     }
  1239.  
  1240.   *ptr++ = sp;
  1241.   *ptr = (struct glosym *) 0;
  1242. }
  1243.  
  1244. int
  1245. set_element_prefixed_p (name)
  1246.      char *name;
  1247. {
  1248.   struct string_list_element *p;
  1249.   int i;
  1250.  
  1251.   for (p = set_element_prefixes; p; p = p->next)
  1252.     {
  1253.       for (i = 0; p->str[i] != '\0' && (p->str[i] == name[i]); i++)
  1254.     ;
  1255.  
  1256.       if (p->str[i] == '\0')
  1257.     return 1;
  1258.     }
  1259.   return 0;
  1260. }
  1261.  
  1262. int parse ();
  1263.  
  1264. /* Record an option and arrange to act on it later.
  1265.    ARG should be the following command argument,
  1266.    which may or may not be used by this option.
  1267.  
  1268.    The `l' and `A' options are ignored here since they actually
  1269.    specify input files.  */
  1270.  
  1271. void
  1272. decode_option (swt, arg)
  1273.      register char *swt, *arg;
  1274. {
  1275.   /* We get Bstatic from gcc on suns.  */
  1276.   if (! strcmp (swt + 1, "Bstatic"))
  1277.     return;
  1278.   if (! strcmp (swt + 1, "Ttext"))
  1279.     {
  1280.       text_start = parse (arg, "%x", "invalid argument to -Ttext");
  1281.       T_flag_specified = 1;
  1282.       return;
  1283.     }
  1284.   if (! strcmp (swt + 1, "Tdata"))
  1285.     {
  1286.       data_start = parse (arg, "%x", "invalid argument to -Tdata");
  1287.       Tdata_flag_specified = 1;
  1288.       return;
  1289.     }
  1290.   if (! strcmp (swt + 1, "noinhibit-exec"))
  1291.     {
  1292.       force_executable = 1;
  1293.       return;
  1294.     }
  1295.  
  1296.   if (swt[2] != 0)
  1297.     arg = &swt[2];
  1298.  
  1299.   switch (swt[1])
  1300.     {
  1301.     case 'A':
  1302.       return;
  1303.  
  1304.     case 'D':
  1305.       specified_data_size = parse (arg, "%x", "invalid argument to -D");
  1306.       return;
  1307.  
  1308.     case 'd':
  1309.       force_common_definition = 1;
  1310.       return;
  1311.  
  1312.     case 'e':
  1313.       entry_symbol = getsym (arg);
  1314.       if (!entry_symbol->defined && !entry_symbol->referenced)
  1315.     undefined_global_sym_count++;
  1316.       entry_symbol->referenced = 1;
  1317.       add_cmdline_ref (entry_symbol);
  1318.       return;
  1319.  
  1320.     case 'l':
  1321.       /* If linking with libg++, use the C++ demangler. */
  1322.       if (arg != NULL && strcmp (arg, "g++") == 0)
  1323.     demangler = cplus_demangle;
  1324.       return;
  1325.  
  1326.     case 'L':
  1327.       n_search_dirs++;
  1328.       search_dirs
  1329.     = (char **) xrealloc (search_dirs, n_search_dirs * sizeof (char *));
  1330.       search_dirs[n_search_dirs - 1] = arg;
  1331.       return;
  1332.  
  1333.     case 'M':
  1334.       write_map = 1;
  1335.       return;
  1336.  
  1337.     case 'N':
  1338.       magic = OMAGIC;
  1339.       return;
  1340.  
  1341. #ifdef NMAGIC
  1342.     case 'n':
  1343.       magic = NMAGIC;
  1344.       return;
  1345. #endif
  1346.  
  1347.     case 'o':
  1348.       output_filename = arg;
  1349.       return;
  1350.  
  1351.     case 'r':
  1352.       relocatable_output = 1;
  1353.       magic = OMAGIC;
  1354.       text_start = 0;
  1355.       return;
  1356.  
  1357.     case 'S':
  1358.       strip_symbols = STRIP_DEBUGGER;
  1359.       return;
  1360.  
  1361.     case 's':
  1362.       strip_symbols = STRIP_ALL;
  1363.       return;
  1364.  
  1365.     case 'T':
  1366.       text_start = parse (arg, "%x", "invalid argument to -T");
  1367.       T_flag_specified = 1;
  1368.       return;
  1369.  
  1370.     case 't':
  1371.       trace_files = 1;
  1372.       return;
  1373.  
  1374.     case 'u':
  1375.       {
  1376.     register symbol *sp = getsym (arg);
  1377.     if (!sp->defined && !sp->referenced)
  1378.       undefined_global_sym_count++;
  1379.     sp->referenced = 1;
  1380.     add_cmdline_ref (sp);
  1381.       }
  1382.       return;
  1383.  
  1384.     case 'V':
  1385.       {
  1386.     struct string_list_element *new
  1387.       = (struct string_list_element *)
  1388.         xmalloc (sizeof (struct string_list_element));
  1389.  
  1390.     new->str = arg;
  1391.     new->next = set_element_prefixes;
  1392.     set_element_prefixes = new;
  1393.     return;
  1394.       }
  1395.  
  1396.     case 'X':
  1397.       discard_locals = DISCARD_L;
  1398.       return;
  1399.  
  1400.     case 'x':
  1401.       discard_locals = DISCARD_ALL;
  1402.       return;
  1403.  
  1404.     case 'y':
  1405.       {
  1406.     register symbol *sp = getsym (&swt[2]);
  1407.     sp->trace = 1;
  1408.       }
  1409.       return;
  1410.  
  1411.     case 'z':
  1412.       magic = ZMAGIC;
  1413.       return;
  1414.  
  1415.     default:
  1416.       fatal ("invalid command option `%s'", swt);
  1417.     }
  1418. }
  1419.  
  1420. /** Convenient functions for operating on one or all files being */
  1421.  /** loaded.  */
  1422. void print_file_name ();
  1423.  
  1424. /* Call FUNCTION on each input file entry.
  1425.    Do not call for entries for libraries;
  1426.    instead, call once for each library member that is being loaded.
  1427.  
  1428.    FUNCTION receives two arguments: the entry, and ARG.  */
  1429.  
  1430. void
  1431. each_file (function, arg)
  1432.      register void (*function)();
  1433.      register int arg;
  1434. {
  1435.   register int i;
  1436.  
  1437.   for (i = 0; i < number_of_files; i++)
  1438.     {
  1439.       register struct file_entry *entry = &file_table[i];
  1440.       if (entry->library_flag)
  1441.         {
  1442.       register struct file_entry *subentry = entry->subfiles;
  1443.       for (; subentry; subentry = subentry->chain)
  1444.         (*function) (subentry, arg);
  1445.     }
  1446.       else
  1447.     (*function) (entry, arg);
  1448.     }
  1449. }
  1450.  
  1451. /* Call FUNCTION on each input file entry until it returns a non-zero
  1452.    value.  Return this value.
  1453.    Do not call for entries for libraries;
  1454.    instead, call once for each library member that is being loaded.
  1455.  
  1456.    FUNCTION receives two arguments: the entry, and ARG.  It must be a
  1457.    function returning unsigned long (though this can probably be fudged). */
  1458.  
  1459. unsigned long
  1460. check_each_file (function, arg)
  1461.      register unsigned long (*function)();
  1462.      register int arg;
  1463. {
  1464.   register int i;
  1465.   register unsigned long return_val;
  1466.  
  1467.   for (i = 0; i < number_of_files; i++)
  1468.     {
  1469.       register struct file_entry *entry = &file_table[i];
  1470.       if (entry->library_flag)
  1471.         {
  1472.       register struct file_entry *subentry = entry->subfiles;
  1473.       for (; subentry; subentry = subentry->chain)
  1474.         if (return_val = (*function) (subentry, arg))
  1475.           return return_val;
  1476.     }
  1477.       else
  1478.     if (return_val = (*function) (entry, arg))
  1479.       return return_val;
  1480.     }
  1481.   return 0;
  1482. }
  1483.  
  1484. /* Like `each_file' but ignore files that were just for symbol definitions.  */
  1485.  
  1486. void
  1487. each_full_file (function, arg)
  1488.      register void (*function)();
  1489.      register int arg;
  1490. {
  1491.   register int i;
  1492.  
  1493.   for (i = 0; i < number_of_files; i++)
  1494.     {
  1495.       register struct file_entry *entry = &file_table[i];
  1496.       if (entry->just_syms_flag)
  1497.     continue;
  1498.       if (entry->library_flag)
  1499.         {
  1500.       register struct file_entry *subentry = entry->subfiles;
  1501.       for (; subentry; subentry = subentry->chain)
  1502.         (*function) (subentry, arg);
  1503.     }
  1504.       else
  1505.     (*function) (entry, arg);
  1506.     }
  1507. }
  1508.  
  1509. /* Close the input file that is now open.  */
  1510.  
  1511. void
  1512. file_close ()
  1513. {
  1514.   close (input_desc);
  1515.   input_desc = 0;
  1516.   input_file = 0;
  1517. }
  1518.  
  1519. /* Open the input file specified by 'entry', and return a descriptor.
  1520.    The open file is remembered; if the same file is opened twice in a row,
  1521.    a new open is not actually done.  */
  1522.  
  1523. int
  1524. file_open (entry)
  1525.      register struct file_entry *entry;
  1526. {
  1527.   register int desc;
  1528.  
  1529.   if (entry->superfile)
  1530.     return file_open (entry->superfile);
  1531.  
  1532.   if (entry == input_file)
  1533.     return input_desc;
  1534.  
  1535.   if (input_file) file_close ();
  1536.  
  1537.   if (entry->search_dirs_flag)
  1538.     {
  1539.       int i;
  1540.  
  1541.       for (i = 0; i < n_search_dirs; i++)
  1542.     {
  1543.       register char *string
  1544.         = concat (search_dirs[i], "/", entry->filename);
  1545.       desc = open (string, O_RDONLY, 0);
  1546.       if (desc > 0)
  1547.         {
  1548.           entry->filename = string;
  1549.           entry->search_dirs_flag = 0;
  1550.           break;
  1551.         }
  1552.       free (string);
  1553.     }
  1554.     }
  1555.   else
  1556.     desc = open (entry->filename, O_RDONLY, 0);
  1557.  
  1558.   if (desc > 0)
  1559.     {
  1560.       input_file = entry;
  1561.       input_desc = desc;
  1562.       return desc;
  1563.     }
  1564.  
  1565.   perror_file (entry);
  1566.   /* NOTREACHED */
  1567. }
  1568.  
  1569. /* Print the filename of ENTRY on OUTFILE (a stdio stream),
  1570.    and then a newline.  */
  1571.  
  1572. void
  1573. prline_file_name (entry, outfile)
  1574.      struct file_entry *entry;
  1575.      FILE *outfile;
  1576. {
  1577.   print_file_name (entry, outfile);
  1578.   fprintf (outfile, "\n");
  1579. }
  1580.  
  1581. /* Print the filename of ENTRY on OUTFILE (a stdio stream).  */
  1582.  
  1583. void
  1584. print_file_name (entry, outfile)
  1585.      struct file_entry *entry;
  1586.      FILE *outfile;
  1587. {
  1588.   if (entry->superfile)
  1589.     {
  1590.       print_file_name (entry->superfile, outfile);
  1591.       fprintf (outfile, "(%s)", entry->filename);
  1592.     }
  1593.   else
  1594.     fprintf (outfile, "%s", entry->filename);
  1595. }
  1596.  
  1597. /* Return the filename of entry as a string (malloc'd for the purpose) */
  1598.  
  1599. char *
  1600. get_file_name (entry)
  1601.      struct file_entry *entry;
  1602. {
  1603.   char *result, *supfile;
  1604.   if (entry->superfile)
  1605.     {
  1606.       supfile = get_file_name (entry->superfile);
  1607.       result = (char *) xmalloc (strlen (supfile)
  1608.                  + strlen (entry->filename) + 3);
  1609.       sprintf (result, "%s(%s)", supfile, entry->filename);
  1610.       free (supfile);
  1611.     }
  1612.   else
  1613.     {
  1614.       result = (char *) xmalloc (strlen (entry->filename) + 1);
  1615.       strcpy (result, entry->filename);
  1616.     }
  1617.   return result;
  1618. }
  1619.  
  1620. /* Medium-level input routines for rel files.  */
  1621.  
  1622. /* Read a file's header into the proper place in the file_entry.
  1623.    DESC is the descriptor on which the file is open.
  1624.    ENTRY is the file's entry.  */
  1625.  
  1626. void
  1627. read_header (desc, entry)
  1628.      int desc;
  1629.      register struct file_entry *entry;
  1630. {
  1631.   register int len;
  1632.   struct exec *loc = (struct exec *) &entry->header;
  1633.  
  1634.   lseek (desc, entry->starting_offset, 0);
  1635. #ifdef COFF_ENCAPSULATE
  1636.   if (entry->just_syms_flag)
  1637.     lseek (desc, sizeof(coffheader), 1);
  1638. #endif
  1639.   len = read (desc, loc, sizeof (struct exec));
  1640.   if (len != sizeof (struct exec))
  1641.     fatal_with_file ("failure reading header of ", entry);
  1642.   if (N_BADMAG (*loc))
  1643.     fatal_with_file ("bad magic number in ", entry);
  1644.  
  1645.   entry->header_read_flag = 1;
  1646. }
  1647.  
  1648. /* Read the symbols of file ENTRY into core.
  1649.    Assume it is already open, on descriptor DESC.
  1650.    Also read the length of the string table, which follows the symbol table,
  1651.    but don't read the contents of the string table.  */
  1652.  
  1653. void
  1654. read_entry_symbols (desc, entry)
  1655.      struct file_entry *entry;
  1656.      int desc;
  1657. {
  1658.   int str_size;
  1659.  
  1660.   if (!entry->header_read_flag)
  1661.     read_header (desc, entry);
  1662.  
  1663.   entry->symbols = (struct nlist *) xmalloc (entry->header.a_syms);
  1664.  
  1665.   lseek (desc, N_SYMOFF (entry->header) + entry->starting_offset, 0);
  1666.   if (entry->header.a_syms != read (desc, entry->symbols, entry->header.a_syms))
  1667.     fatal_with_file ("premature end of file in symbols of ", entry);
  1668.  
  1669.   lseek (desc, N_STROFF (entry->header) + entry->starting_offset, 0);
  1670.   if (sizeof str_size != read (desc, &str_size, sizeof str_size))
  1671.     fatal_with_file ("bad string table size in ", entry);
  1672.  
  1673.   entry->string_size = str_size;
  1674. }
  1675.  
  1676. /* Read the string table of file ENTRY into core.
  1677.    Assume it is already open, on descriptor DESC.
  1678.    Also record whether a GDB symbol segment follows the string table.  */
  1679.  
  1680. void
  1681. read_entry_strings (desc, entry)
  1682.      struct file_entry *entry;
  1683.      int desc;
  1684. {
  1685.   int buffer;
  1686.  
  1687.   if (!entry->header_read_flag)
  1688.     read_header (desc, entry);
  1689.  
  1690.   lseek (desc, N_STROFF (entry->header) + entry->starting_offset, 0);
  1691.   if (entry->string_size != read (desc, entry->strings, entry->string_size))
  1692.     fatal_with_file ("premature end of file in strings of ", entry);
  1693.  
  1694.   /* While we are here, see if the file has a symbol segment at the end.
  1695.      For a separate file, just try reading some more.
  1696.      For a library member, compare current pos against total size.  */
  1697.   if (entry->superfile)
  1698.     {
  1699.       if (entry->total_size == N_STROFF (entry->header) + entry->string_size)
  1700.     return;
  1701.     }
  1702.   else
  1703.     {
  1704.       buffer = read (desc, &buffer, sizeof buffer);
  1705.       if (buffer == 0)
  1706.     return;
  1707.       if (buffer != sizeof buffer)
  1708.     fatal_with_file ("premature end of file in GDB symbol segment of ", entry);
  1709.     }
  1710.   /* Don't try to do anything with symsegs.  */
  1711.   return;
  1712. #if 0
  1713.   /* eliminate warning of `statement not reached'.  */
  1714.   entry->symseg_offset = N_STROFF (entry->header) + entry->string_size;
  1715. #endif
  1716. }
  1717.  
  1718. /* Read in the symbols of all input files.  */
  1719.  
  1720. void read_file_symbols (), read_entry_symbols (), read_entry_strings ();
  1721. void enter_file_symbols (), enter_global_ref (), search_library ();
  1722.  
  1723. void
  1724. load_symbols ()
  1725. {
  1726.   register int i;
  1727.  
  1728.   if (trace_files) fprintf (stderr, "Loading symbols:\n\n");
  1729.  
  1730.   for (i = 0; i < number_of_files; i++)
  1731.     {
  1732.       register struct file_entry *entry = &file_table[i];
  1733.       read_file_symbols (entry);
  1734.     }
  1735.  
  1736.   if (trace_files) fprintf (stderr, "\n");
  1737. }
  1738.  
  1739. /* If ENTRY is a rel file, read its symbol and string sections into core.
  1740.    If it is a library, search it and load the appropriate members
  1741.    (which means calling this function recursively on those members).  */
  1742.  
  1743. void
  1744. read_file_symbols (entry)
  1745.      register struct file_entry *entry;
  1746. {
  1747.   register int desc;
  1748.   register int len;
  1749.   struct exec hdr;
  1750.  
  1751.   desc = file_open (entry);
  1752.  
  1753. #ifdef COFF_ENCAPSULATE
  1754.   if (entry->just_syms_flag)
  1755.     lseek (desc, sizeof(coffheader),0);
  1756. #endif
  1757.  
  1758.   len = read (desc, &hdr, sizeof hdr);
  1759.   if (len != sizeof hdr)
  1760.     fatal_with_file ("failure reading header of ", entry);
  1761.  
  1762.   if (!N_BADMAG (hdr))
  1763.     {
  1764.       read_entry_symbols (desc, entry);
  1765.       entry->strings = (char *) alloca (entry->string_size);
  1766.       read_entry_strings (desc, entry);
  1767.       enter_file_symbols (entry);
  1768.       entry->strings = 0;
  1769.     }
  1770.   else
  1771.     {
  1772.       char armag[SARMAG];
  1773.  
  1774.       lseek (desc, 0, 0);
  1775.       if (SARMAG != read (desc, armag, SARMAG) || strncmp (armag, ARMAG, SARMAG))
  1776.     fatal_with_file ("malformed input file (not rel or archive) ", entry);
  1777.       entry->library_flag = 1;
  1778.       search_library (desc, entry);
  1779.     }
  1780.  
  1781.   file_close ();
  1782. }
  1783.  
  1784. /* Enter the external symbol defs and refs of ENTRY in the hash table.  */
  1785.  
  1786. void
  1787. enter_file_symbols (entry)
  1788.      struct file_entry *entry;
  1789. {
  1790.   register struct nlist
  1791.     *p,
  1792.     *end = entry->symbols + entry->header.a_syms / sizeof (struct nlist);
  1793.  
  1794.   if (trace_files) prline_file_name (entry, stderr);
  1795.  
  1796.   for (p = entry->symbols; p < end; p++)
  1797.     {
  1798.       if (p->n_type == (N_SETV | N_EXT)) continue;
  1799.       if (set_element_prefixes
  1800.       && set_element_prefixed_p (p->n_un.n_strx + entry->strings))
  1801.     p->n_type += (N_SETA - N_ABS);
  1802.  
  1803.       if (SET_ELEMENT_P (p->n_type))
  1804.     {
  1805.       set_symbol_count++;
  1806.       if (!relocatable_output)
  1807.         enter_global_ref (p, p->n_un.n_strx + entry->strings, entry);
  1808.     }
  1809.       else if (p->n_type == N_WARNING)
  1810.     {
  1811.       char *name = p->n_un.n_strx + entry->strings;
  1812.  
  1813.       /* Grab the next entry.  */
  1814.       p++;
  1815.       if (p->n_type != (N_UNDF | N_EXT))
  1816.         {
  1817.           fprintf (stderr, "%s: Warning symbol found in %s without external reference following.\n",
  1818.                progname, entry->filename);
  1819.           make_executable = 0;
  1820.           p--;        /* Process normally.  */
  1821.         }
  1822.       else
  1823.         {
  1824.           symbol *sp;
  1825.           char *sname = p->n_un.n_strx + entry->strings;
  1826.           /* Deal with the warning symbol.  */
  1827.           enter_global_ref (p, p->n_un.n_strx + entry->strings, entry);
  1828.           sp = getsym (sname);
  1829.           sp->warning = (char *) xmalloc (strlen(name) + 1);
  1830.           strcpy (sp->warning, name);
  1831.           warning_count++;
  1832.         }
  1833.     }
  1834.       else if (p->n_type & N_EXT)
  1835.     enter_global_ref (p, p->n_un.n_strx + entry->strings, entry);
  1836.       else if (p->n_un.n_strx && !(p->n_type & (N_STAB | N_EXT)))
  1837.     {
  1838.       if ((p->n_un.n_strx + entry->strings)[0] != LPREFIX)
  1839.         non_L_local_sym_count++;
  1840.       local_sym_count++;
  1841.     }
  1842.       else debugger_sym_count++;
  1843.     }
  1844.  
  1845.    /* Count one for the local symbol that we generate,
  1846.       whose name is the file's name (usually) and whose address
  1847.       is the start of the file's text.  */
  1848.  
  1849.   local_sym_count++;
  1850.   non_L_local_sym_count++;
  1851. }
  1852.  
  1853. /* Enter one global symbol in the hash table.
  1854.    NLIST_P points to the `struct nlist' read from the file
  1855.    that describes the global symbol.  NAME is the symbol's name.
  1856.    ENTRY is the file entry for the file the symbol comes from.
  1857.  
  1858.    The `struct nlist' is modified by placing it on a chain of
  1859.    all such structs that refer to the same global symbol.
  1860.    This chain starts in the `refs' field of the symbol table entry
  1861.    and is chained through the `n_name'.  */
  1862.  
  1863. void
  1864. enter_global_ref (nlist_p, name, entry)
  1865.      register struct nlist *nlist_p;
  1866.      char *name;
  1867.      struct file_entry *entry;
  1868. {
  1869.   register symbol *sp = getsym (name);
  1870.   register int type = nlist_p->n_type;
  1871.   int oldref = sp->referenced;
  1872.   int olddef = sp->defined;
  1873.  
  1874.   nlist_p->n_un.n_name = (char *) sp->refs;
  1875.   sp->refs = nlist_p;
  1876.  
  1877.   sp->referenced = 1;
  1878.   if (type != (N_UNDF | N_EXT) || nlist_p->n_value)
  1879.     {
  1880.       if (!sp->defined || sp->defined == (N_UNDF | N_EXT))
  1881.     sp->defined = type;
  1882.  
  1883.       if (oldref && !olddef)
  1884.     /* It used to be undefined and we're defining it.  */
  1885.     undefined_global_sym_count--;
  1886.  
  1887.       if (!olddef && type == (N_UNDF | N_EXT) && nlist_p->n_value)
  1888.     {
  1889.       /* First definition and it's common.  */
  1890.       common_defined_global_count++;
  1891.       sp->max_common_size = nlist_p->n_value;
  1892.     }
  1893.       else if (olddef && sp->max_common_size && type != (N_UNDF | N_EXT))
  1894.     {
  1895.       /* It used to be common and we're defining it as
  1896.          something else.  */
  1897.       common_defined_global_count--;
  1898.       sp->max_common_size = 0;
  1899.     }
  1900.       else if (olddef && sp->max_common_size && type == (N_UNDF | N_EXT)
  1901.       && sp->max_common_size < nlist_p->n_value)
  1902.     /* It used to be common and this is a new common entry to
  1903.        which we need to pay attention.  */
  1904.     sp->max_common_size = nlist_p->n_value;
  1905.  
  1906.       /* Are we defining it as a set element?  */
  1907.       if (SET_ELEMENT_P (type)
  1908.       && (!olddef || (olddef && sp->max_common_size)))
  1909.     set_vector_count++;
  1910.       /* As an indirection?  */
  1911.       else if (type == (N_INDR | N_EXT))
  1912.     {
  1913.       /* Indirect symbols value should be modified to point
  1914.          a symbol being equivalenced to. */
  1915.       nlist_p->n_value
  1916.         = (unsigned int) getsym ((nlist_p + 1)->n_un.n_strx
  1917.                      + entry->strings);
  1918.       if ((symbol *) nlist_p->n_value == sp)
  1919.         {
  1920.           /* Somebody redefined a symbol to be itself.  */
  1921.           fprintf (stderr, "%s: Symbol %s indirected to itself.\n",
  1922.                entry->filename, name);
  1923.           /* Rewrite this symbol as being a global text symbol
  1924.          with value 0.  */
  1925.           nlist_p->n_type = sp->defined = N_TEXT | N_EXT;
  1926.           nlist_p->n_value = 0;
  1927.           /* Don't make the output executable.  */
  1928.           make_executable = 0;
  1929.         }
  1930.       else
  1931.         global_indirect_count++;
  1932.     }
  1933.     }
  1934.   else
  1935.     if (!oldref)
  1936. #ifndef DOLLAR_KLUDGE
  1937.       undefined_global_sym_count++;
  1938. #else
  1939.       {
  1940.     if (entry->superfile && type == (N_UNDF | N_EXT) && name[1] == '$')
  1941.       {
  1942.         /* This is an (ISI?) $-conditional; skip it */
  1943.         sp->referenced = 0;
  1944.         if (sp->trace)
  1945.           {
  1946.         fprintf (stderr, "symbol %s is a $-conditional ignored in ", sp->name);
  1947.         print_file_name (entry, stderr);
  1948.         fprintf (stderr, "\n");
  1949.           }
  1950.         return;
  1951.       }
  1952.     else
  1953.       undefined_global_sym_count++;
  1954.       }
  1955. #endif
  1956.  
  1957.   if (sp == end_symbol && entry->just_syms_flag && !T_flag_specified)
  1958.     text_start = nlist_p->n_value;
  1959.  
  1960.   if (sp->trace)
  1961.     {
  1962.       register char *reftype;
  1963.       switch (type & N_TYPE)
  1964.     {
  1965.     case N_UNDF:
  1966.       if (nlist_p->n_value)
  1967.         reftype = "defined as common";
  1968.       else reftype = "referenced";
  1969.       break;
  1970.  
  1971.     case N_ABS:
  1972.       reftype = "defined as absolute";
  1973.       break;
  1974.  
  1975.     case N_TEXT:
  1976.       reftype = "defined in text section";
  1977.       break;
  1978.  
  1979.     case N_DATA:
  1980.       reftype = "defined in data section";
  1981.       break;
  1982.  
  1983.     case N_BSS:
  1984.       reftype = "defined in BSS section";
  1985.       break;
  1986.  
  1987.     case N_SETT:
  1988.       reftype = "is a text set element";
  1989.       break;
  1990.  
  1991.     case N_SETD:
  1992.       reftype = "is a data set element";
  1993.       break;
  1994.  
  1995.     case N_SETB:
  1996.       reftype = "is a BSS set element";
  1997.       break;
  1998.  
  1999.     case N_SETA:
  2000.       reftype = "is an absolute set element";
  2001.       break;
  2002.  
  2003.     case N_SETV:
  2004.       reftype = "defined in data section as vector";
  2005.       break;
  2006.  
  2007.     case N_INDR:
  2008.       reftype = (char *) alloca (23
  2009.                      + strlen ((nlist_p + 1)->n_un.n_strx
  2010.                            + entry->strings));
  2011.       sprintf (reftype, "defined equivalent to %s",
  2012.            (nlist_p + 1)->n_un.n_strx + entry->strings);
  2013.       break;
  2014.  
  2015. #ifdef sequent
  2016.     case N_SHUNDF:
  2017.       reftype = "shared undf";
  2018.       break;
  2019.  
  2020. /* These conflict with cases above.
  2021.     case N_SHDATA:
  2022.       reftype = "shared data";
  2023.       break;
  2024.  
  2025.     case N_SHBSS:
  2026.       reftype = "shared BSS";
  2027.       break;
  2028. */
  2029.     default:
  2030.       reftype = "I don't know this type";
  2031.       break;
  2032. #endif
  2033.     }
  2034.  
  2035.       fprintf (stderr, "symbol %s %s in ", sp->name, reftype);
  2036.       print_file_name (entry, stderr);
  2037.       fprintf (stderr, "\n");
  2038.     }
  2039. }
  2040.  
  2041. /* This return 0 if the given file entry's symbol table does *not*
  2042.    contain the nlist point entry, and it returns the files entry
  2043.    pointer (cast to unsigned long) if it does. */
  2044.  
  2045. unsigned long
  2046. contains_symbol (entry, n_ptr)
  2047.      struct file_entry *entry;
  2048.      register struct nlist *n_ptr;
  2049. {
  2050.   if (n_ptr >= entry->symbols &&
  2051.       n_ptr < (entry->symbols
  2052.            + (entry->header.a_syms / sizeof (struct nlist))))
  2053.     return (unsigned long) entry;
  2054.   return 0;
  2055. }
  2056.  
  2057.  
  2058. /* Searching libraries */
  2059.  
  2060. struct file_entry *decode_library_subfile ();
  2061. void linear_library (), symdef_library ();
  2062.  
  2063. /* Search the library ENTRY, already open on descriptor DESC.
  2064.    This means deciding which library members to load,
  2065.    making a chain of `struct file_entry' for those members,
  2066.    and entering their global symbols in the hash table.  */
  2067.  
  2068. void
  2069. search_library (desc, entry)
  2070.      int desc;
  2071.      struct file_entry *entry;
  2072. {
  2073.   int member_length;
  2074.   register char *name;
  2075.   register struct file_entry *subentry;
  2076.  
  2077.   if (!undefined_global_sym_count) return;
  2078.  
  2079.   /* Examine its first member, which starts SARMAG bytes in.  */
  2080.   subentry = decode_library_subfile (desc, entry, SARMAG, &member_length);
  2081.   if (!subentry) return;
  2082.  
  2083.   name = subentry->filename;
  2084.   free (subentry);
  2085.  
  2086.   /* Search via __.SYMDEF if that exists, else linearly.  */
  2087.  
  2088.   if (!strcmp (name, "__.SYMDEF"))
  2089.     symdef_library (desc, entry, member_length);
  2090.   else
  2091.     linear_library (desc, entry);
  2092. }
  2093.  
  2094. /* Construct and return a file_entry for a library member.
  2095.    The library's file_entry is library_entry, and the library is open on DESC.
  2096.    SUBFILE_OFFSET is the byte index in the library of this member's header.
  2097.    We store the length of the member into *LENGTH_LOC.  */
  2098.  
  2099. struct file_entry *
  2100. decode_library_subfile (desc, library_entry, subfile_offset, length_loc)
  2101.      int desc;
  2102.      struct file_entry *library_entry;
  2103.      int subfile_offset;
  2104.      int *length_loc;
  2105. {
  2106.   int bytes_read;
  2107.   register int namelen;
  2108.   int member_length;
  2109.   register char *name;
  2110.   struct ar_hdr hdr1;
  2111.   register struct file_entry *subentry;
  2112.  
  2113.   lseek (desc, subfile_offset, 0);
  2114.  
  2115.   bytes_read = read (desc, &hdr1, sizeof hdr1);
  2116.   if (!bytes_read)
  2117.     return 0;        /* end of archive */
  2118.  
  2119.   if (sizeof hdr1 != bytes_read)
  2120.     fatal_with_file ("malformed library archive ", library_entry);
  2121.  
  2122.   if (sscanf (hdr1.ar_size, "%d", &member_length) != 1)
  2123.     fatal_with_file ("malformatted header of archive member in ", library_entry);
  2124.  
  2125.   subentry = (struct file_entry *) xmalloc (sizeof (struct file_entry));
  2126.   bzero (subentry, sizeof (struct file_entry));
  2127.  
  2128.   for (namelen = 0;
  2129.        namelen < sizeof hdr1.ar_name
  2130.        && hdr1.ar_name[namelen] != 0 && hdr1.ar_name[namelen] != ' '
  2131.        && hdr1.ar_name[namelen] != '/';
  2132.        namelen++);
  2133.  
  2134.   name = (char *) xmalloc (namelen+1);
  2135.   strncpy (name, hdr1.ar_name, namelen);
  2136.   name[namelen] = 0;
  2137.  
  2138.   subentry->filename = name;
  2139.   subentry->local_sym_name = name;
  2140.   subentry->symbols = 0;
  2141.   subentry->strings = 0;
  2142.   subentry->subfiles = 0;
  2143.   subentry->starting_offset = subfile_offset + sizeof hdr1;
  2144.   subentry->superfile = library_entry;
  2145.   subentry->library_flag = 0;
  2146.   subentry->header_read_flag = 0;
  2147.   subentry->just_syms_flag = 0;
  2148.   subentry->chain = 0;
  2149.   subentry->total_size = member_length;
  2150.  
  2151.   (*length_loc) = member_length;
  2152.  
  2153.   return subentry;
  2154. }
  2155.  
  2156. int subfile_wanted_p ();
  2157.  
  2158. /* Search a library that has a __.SYMDEF member.
  2159.    DESC is a descriptor on which the library is open.
  2160.      The file pointer is assumed to point at the __.SYMDEF data.
  2161.    ENTRY is the library's file_entry.
  2162.    MEMBER_LENGTH is the length of the __.SYMDEF data.  */
  2163.  
  2164. void
  2165. symdef_library (desc, entry, member_length)
  2166.      int desc;
  2167.      struct file_entry *entry;
  2168.      int member_length;
  2169. {
  2170.   int *symdef_data = (int *) xmalloc (member_length);
  2171.   register struct symdef *symdef_base;
  2172.   char *sym_name_base;
  2173.   int number_of_symdefs;
  2174.   int length_of_strings;
  2175.   int not_finished;
  2176.   int bytes_read;
  2177.   register int i;
  2178.   struct file_entry *prev = 0;
  2179.   int prev_offset = 0;
  2180.  
  2181.   bytes_read = read (desc, symdef_data, member_length);
  2182.   if (bytes_read != member_length)
  2183.     fatal_with_file ("malformatted __.SYMDEF in ", entry);
  2184.  
  2185.   number_of_symdefs = *symdef_data / sizeof (struct symdef);
  2186.   if (number_of_symdefs < 0 ||
  2187.        number_of_symdefs * sizeof (struct symdef) + 2 * sizeof (int) > member_length)
  2188.     fatal_with_file ("malformatted __.SYMDEF in ", entry);
  2189.  
  2190.   symdef_base = (struct symdef *) (symdef_data + 1);
  2191.   length_of_strings = *(int *) (symdef_base + number_of_symdefs);
  2192.  
  2193.   if (length_of_strings < 0
  2194.       || number_of_symdefs * sizeof (struct symdef) + length_of_strings
  2195.       + 2 * sizeof (int) != member_length)
  2196.     fatal_with_file ("malformatted __.SYMDEF in ", entry);
  2197.  
  2198.   sym_name_base = sizeof (int) + (char *) (symdef_base + number_of_symdefs);
  2199.  
  2200.   /* Check all the string indexes for validity.  */
  2201.  
  2202.   for (i = 0; i < number_of_symdefs; i++)
  2203.     {
  2204.       register int index = symdef_base[i].symbol_name_string_index;
  2205.       if (index < 0 || index >= length_of_strings
  2206.       || (index && *(sym_name_base + index - 1)))
  2207.     fatal_with_file ("malformatted __.SYMDEF in ", entry);
  2208.     }
  2209.  
  2210.   /* Search the symdef data for members to load.
  2211.      Do this until one whole pass finds nothing to load.  */
  2212.  
  2213.   not_finished = 1;
  2214.   while (not_finished)
  2215.     {
  2216.       not_finished = 0;
  2217.  
  2218.       /* Scan all the symbols mentioned in the symdef for ones that we need.
  2219.      Load the library members that contain such symbols.  */
  2220.  
  2221.       for (i = 0;
  2222.        (i < number_of_symdefs
  2223.         && (undefined_global_sym_count || common_defined_global_count));
  2224.        i++)
  2225.     if (symdef_base[i].symbol_name_string_index >= 0)
  2226.       {
  2227.         register symbol *sp;
  2228.  
  2229.         sp = getsym_soft (sym_name_base
  2230.                   + symdef_base[i].symbol_name_string_index);
  2231.  
  2232.         /* If we find a symbol that appears to be needed, think carefully
  2233.            about the archive member that the symbol is in.  */
  2234.  
  2235.         if (sp && ((sp->referenced && !sp->defined)
  2236.                || (sp->defined && sp->max_common_size)))
  2237.           {
  2238.         int junk;
  2239.         register int j;
  2240.         register int offset = symdef_base[i].library_member_offset;
  2241.         struct file_entry *subentry;
  2242.  
  2243.         /* Don't think carefully about any archive member
  2244.            more than once in a given pass.  */
  2245.  
  2246.         if (prev_offset == offset)
  2247.           continue;
  2248.         prev_offset = offset;
  2249.  
  2250.         /* Read the symbol table of the archive member.  */
  2251.  
  2252.         subentry = decode_library_subfile (desc, entry, offset, &junk);
  2253.         if (subentry == 0)
  2254.           fatal ("invalid offset for %s in symbol table of %s",
  2255.              sym_name_base
  2256.              + symdef_base[i].symbol_name_string_index,
  2257.              entry->filename);
  2258.         read_entry_symbols (desc, subentry);
  2259.         subentry->strings = (char *) malloc (subentry->string_size);
  2260.         read_entry_strings (desc, subentry);
  2261.  
  2262.         /* Now scan the symbol table and decide whether to load.  */
  2263.  
  2264.         if (!subfile_wanted_p (subentry))
  2265.           {
  2266.             free (subentry->symbols);
  2267.             free (subentry);
  2268.           }
  2269.         else
  2270.           {
  2271.             /* This member is needed; load it.
  2272.                Since we are loading something on this pass,
  2273.                we must make another pass through the symdef data.  */
  2274.  
  2275.             not_finished = 1;
  2276.  
  2277.             enter_file_symbols (subentry);
  2278.  
  2279.             if (prev)
  2280.               prev->chain = subentry;
  2281.             else entry->subfiles = subentry;
  2282.             prev = subentry;
  2283.  
  2284.             /* Clear out this member's symbols from the symdef data
  2285.                so that following passes won't waste time on them.  */
  2286.  
  2287.             for (j = 0; j < number_of_symdefs; j++)
  2288.               {
  2289.             if (symdef_base[j].library_member_offset == offset)
  2290.               symdef_base[j].symbol_name_string_index = -1;
  2291.               }
  2292.           }
  2293.  
  2294.         /* We'll read the strings again if we need them again.  */
  2295.         free (subentry->strings);
  2296.         subentry->strings = 0;
  2297.           }
  2298.       }
  2299.     }
  2300.  
  2301.   free (symdef_data);
  2302. }
  2303.  
  2304. /* Search a library that has no __.SYMDEF.
  2305.    ENTRY is the library's file_entry.
  2306.    DESC is the descriptor it is open on.  */
  2307.  
  2308. void
  2309. linear_library (desc, entry)
  2310.      int desc;
  2311.      struct file_entry *entry;
  2312. {
  2313.   register struct file_entry *prev = 0;
  2314.   register int this_subfile_offset = SARMAG;
  2315.  
  2316.   while (undefined_global_sym_count || common_defined_global_count)
  2317.     {
  2318.       int member_length;
  2319.       register struct file_entry *subentry;
  2320.  
  2321.       subentry = decode_library_subfile (desc, entry, this_subfile_offset,
  2322.                      &member_length);
  2323.  
  2324.       if (!subentry) return;
  2325.  
  2326.       read_entry_symbols (desc, subentry);
  2327.       subentry->strings = (char *) alloca (subentry->string_size);
  2328.       read_entry_strings (desc, subentry);
  2329.  
  2330.       if (!subfile_wanted_p (subentry))
  2331.     {
  2332.       free (subentry->symbols);
  2333.       free (subentry);
  2334.     }
  2335.       else
  2336.     {
  2337.       enter_file_symbols (subentry);
  2338.  
  2339.       if (prev)
  2340.         prev->chain = subentry;
  2341.       else entry->subfiles = subentry;
  2342.       prev = subentry;
  2343.       subentry->strings = 0; /* Since space will dissapear on return */
  2344.     }
  2345.  
  2346.       this_subfile_offset += member_length + sizeof (struct ar_hdr);
  2347.       if (this_subfile_offset & 1) this_subfile_offset++;
  2348.     }
  2349. }
  2350.  
  2351. /* ENTRY is an entry for a library member.
  2352.    Its symbols have been read into core, but not entered.
  2353.    Return nonzero if we ought to load this member.  */
  2354.  
  2355. int
  2356. subfile_wanted_p (entry)
  2357.      struct file_entry *entry;
  2358. {
  2359.   register struct nlist *p;
  2360.   register struct nlist *end
  2361.     = entry->symbols + entry->header.a_syms / sizeof (struct nlist);
  2362. #ifdef DOLLAR_KLUDGE
  2363.   register int dollar_cond = 0;
  2364. #endif
  2365.  
  2366.   for (p = entry->symbols; p < end; p++)
  2367.     {
  2368.       register int type = p->n_type;
  2369.       register char *name = p->n_un.n_strx + entry->strings;
  2370.  
  2371.       /* If the symbol has an interesting definition, we could
  2372.      potentially want it.  */
  2373.       if (type & N_EXT
  2374.       && (type != (N_UNDF | N_EXT) || p->n_value
  2375.  
  2376. #ifdef DOLLAR_KLUDGE
  2377.            || name[1] == '$'
  2378. #endif
  2379.           )
  2380.       && !SET_ELEMENT_P (type)
  2381.       && !set_element_prefixed_p (name))
  2382.     {
  2383.       register symbol *sp = getsym_soft (name);
  2384.  
  2385. #ifdef DOLLAR_KLUDGE
  2386.       if (name[1] == '$')
  2387.         {
  2388.           sp = getsym_soft (&name[2]);
  2389.           dollar_cond = 1;
  2390.           if (!sp) continue;
  2391.           if (sp->referenced)
  2392.         {
  2393.           if (write_map)
  2394.             {
  2395.               print_file_name (entry, stdout);
  2396.               fprintf (stdout, " needed due to $-conditional %s\n", name);
  2397.             }
  2398.           return 1;
  2399.         }
  2400.           continue;
  2401.         }
  2402. #endif
  2403.  
  2404.       /* If this symbol has not been hashed, we can't be looking for it. */
  2405.  
  2406.       if (!sp) continue;
  2407.  
  2408.       if ((sp->referenced && !sp->defined)
  2409.           || (sp->defined && sp->max_common_size))
  2410.         {
  2411.           /* This is a symbol we are looking for.  It is either
  2412.              not yet defined or defined as a common.  */
  2413. #ifdef DOLLAR_KLUDGE
  2414.           if (dollar_cond) continue;
  2415. #endif
  2416.           if (type == (N_UNDF | N_EXT))
  2417.         {
  2418.           /* Symbol being defined as common.
  2419.              Remember this, but don't load subfile just for this.  */
  2420.  
  2421.           /* If it didn't used to be common, up the count of
  2422.              common symbols.  */
  2423.           if (!sp->max_common_size)
  2424.             common_defined_global_count++;
  2425.  
  2426.           if (sp->max_common_size < p->n_value)
  2427.             sp->max_common_size = p->n_value;
  2428.           if (!sp->defined)
  2429.             undefined_global_sym_count--;
  2430.           sp->defined = 1;
  2431.           continue;
  2432.         }
  2433.  
  2434.           if (write_map)
  2435.         {
  2436.           print_file_name (entry, stdout);
  2437.           fprintf (stdout, " needed due to %s\n", sp->name);
  2438.         }
  2439.           return 1;
  2440.         }
  2441.     }
  2442.     }
  2443.  
  2444.   return 0;
  2445. }
  2446.  
  2447. void consider_file_section_lengths (), relocate_file_addresses ();
  2448.  
  2449. /* Having entered all the global symbols and found the sizes of sections
  2450.    of all files to be linked, make all appropriate deductions from this data.
  2451.  
  2452.    We propagate global symbol values from definitions to references.
  2453.    We compute the layout of the output file and where each input file's
  2454.    contents fit into it.  */
  2455.  
  2456. void
  2457. digest_symbols ()
  2458. {
  2459.   register int i;
  2460.   int setv_fill_count;
  2461.  
  2462.   if (trace_files)
  2463.     fprintf (stderr, "Digesting symbol information:\n\n");
  2464.  
  2465.   /* Compute total size of sections */
  2466.  
  2467.   each_file (consider_file_section_lengths, 0);
  2468.  
  2469.   /* If necessary, pad text section to full page in the file.
  2470.      Include the padding in the text segment size.  */
  2471.  
  2472. #ifdef NMAGIC
  2473.   if (magic == ZMAGIC || magic == NMAGIC)
  2474. #else
  2475.   if (magic == ZMAGIC)
  2476. #endif
  2477.     {
  2478.       int text_end = text_size + N_TXTOFF (outheader);
  2479.       text_pad = ((text_end + page_size - 1) & (- page_size)) - text_end;
  2480.       text_size += text_pad;
  2481.     }
  2482.  
  2483. #ifdef _N_BASEADDR
  2484.   /* SunOS 4.1 N_TXTADDR depends on the value of outheader.a_entry.  */
  2485.   outheader.a_entry = N_PAGSIZ (outheader);
  2486. #endif
  2487.  
  2488.   outheader.a_text = text_size;
  2489. #ifdef sequent
  2490.   outheader.a_text += N_ADDRADJ (outheader);
  2491. #endif
  2492.  
  2493.   /* Make the data segment address start in memory on a suitable boundary.  */
  2494.  
  2495.   if (! Tdata_flag_specified)
  2496.     data_start = N_DATADDR (outheader) + text_start - TEXT_START (outheader);
  2497.  
  2498.   /* Set up the set element vector */
  2499.  
  2500.   if (!relocatable_output)
  2501.     {
  2502.       /* The set sector size is the number of set elements + a word
  2503.          for each symbol for the length word at the beginning of the
  2504.      vector, plus a word for each symbol for a zero at the end of
  2505.      the vector (for incremental linking).  */
  2506.       set_sect_size
  2507.     = (2 * set_symbol_count + set_vector_count) * sizeof (unsigned long);
  2508.       set_sect_start = data_start + data_size;
  2509.       data_size += set_sect_size;
  2510.       set_vectors = (unsigned long *) xmalloc (set_sect_size);
  2511.       setv_fill_count = 0;
  2512.     }
  2513.  
  2514.   /* Compute start addresses of each file's sections and symbols.  */
  2515.  
  2516.   each_full_file (relocate_file_addresses, 0);
  2517.  
  2518.   /* Now, for each symbol, verify that it is defined globally at most once.
  2519.      Put the global value into the symbol entry.
  2520.      Common symbols are allocated here, in the BSS section.
  2521.      Each defined symbol is given a '->defined' field
  2522.       which is the correct N_ code for its definition,
  2523.       except in the case of common symbols with -r.
  2524.      Then make all the references point at the symbol entry
  2525.      instead of being chained together. */
  2526.  
  2527.   defined_global_sym_count = 0;
  2528.  
  2529.   for (i = 0; i < TABSIZE; i++)
  2530.     {
  2531.       register symbol *sp;
  2532.       for (sp = symtab[i]; sp; sp = sp->link)
  2533.     {
  2534.       /* For each symbol */
  2535.       register struct nlist *p, *next;
  2536.       int defs = 0, com = sp->max_common_size;
  2537.       struct nlist *first_definition;
  2538.       for (p = sp->refs; p; p = next)
  2539.         {
  2540.           register int type = p->n_type;
  2541.  
  2542.           if (SET_ELEMENT_P (type))
  2543.         {
  2544.           if (relocatable_output)
  2545.             fatal ("internal: global ref to set element with -r");
  2546.           if (!defs++)
  2547.             {
  2548.               sp->value = set_sect_start
  2549.             + setv_fill_count++ * sizeof (unsigned long);
  2550.               sp->defined = N_SETV | N_EXT;
  2551.               first_definition = p;
  2552.             }
  2553.           else if ((sp->defined & ~N_EXT) != N_SETV)
  2554.             {
  2555.               sp->multiply_defined = 1;
  2556.               multiple_def_count++;
  2557.             }
  2558.           set_vectors[setv_fill_count++] = p->n_value;
  2559.         }
  2560.           else if ((type & N_EXT) && type != (N_UNDF | N_EXT))
  2561.         {
  2562.           /* non-common definition */
  2563.           if (defs++ && sp->value != p->n_value)
  2564.             {
  2565.               sp->multiply_defined = 1;
  2566.               multiple_def_count++;
  2567.             }
  2568.           sp->value = p->n_value;
  2569.           sp->defined = type;
  2570.           first_definition = p;
  2571.         }
  2572.           next = (struct nlist *) p->n_un.n_name;
  2573.           p->n_un.n_name = (char *) sp;
  2574.         }
  2575.       /* Allocate as common if defined as common and not defined for real */
  2576.       if (com && !defs)
  2577.         {
  2578.           if (!relocatable_output || force_common_definition)
  2579.         {
  2580.           int align = sizeof (int);
  2581.  
  2582.           /* Round up to nearest sizeof (int).  I don't know
  2583.              whether this is necessary or not (given that
  2584.              alignment is taken care of later), but it's
  2585.              traditional, so I'll leave it in.  Note that if
  2586.              this size alignment is ever removed, ALIGN above
  2587.              will have to be initialized to 1 instead of
  2588.              sizeof (int).  */
  2589.  
  2590.           com = (com + sizeof (int) - 1) & (- sizeof (int));
  2591.  
  2592.           while (!(com & align))
  2593.             align <<= 1;
  2594.  
  2595.           align = align > MAX_ALIGNMENT ? MAX_ALIGNMENT : align;
  2596.  
  2597.           bss_size = ((((bss_size + data_size + data_start)
  2598.                   + (align - 1)) & (- align))
  2599.                   - data_size - data_start);
  2600.  
  2601.           sp->value = data_start + data_size + bss_size;
  2602.           sp->defined = N_BSS | N_EXT;
  2603.           bss_size += com;
  2604.           if (write_map)
  2605.             printf ("Allocating common %s: %x at %x\n",
  2606.                 sp->name, com, sp->value);
  2607.         }
  2608.           else
  2609.         {
  2610.           sp->defined = 0;
  2611.           undefined_global_sym_count++;
  2612.         }
  2613.         }
  2614.       /* Set length word at front of vector and zero byte at end.
  2615.          Reverse the vector itself to put it in file order.  */
  2616.       if ((sp->defined & ~N_EXT) == N_SETV)
  2617.         {
  2618.           unsigned long length_word_index
  2619.         = (sp->value - set_sect_start) / sizeof (unsigned long);
  2620.           unsigned long i, tmp;
  2621.  
  2622.           set_vectors[length_word_index]
  2623.         = setv_fill_count - 1 - length_word_index;
  2624.  
  2625.           /* Reverse the vector.  */
  2626.           for (i = 1;
  2627.            i < (setv_fill_count - length_word_index - 1) / 2 + 1;
  2628.            i++)
  2629.         {
  2630.           tmp = set_vectors[length_word_index + i];
  2631.           set_vectors[length_word_index + i]
  2632.             = set_vectors[setv_fill_count - i];
  2633.           set_vectors[setv_fill_count - i] = tmp;
  2634.         }
  2635.  
  2636.           set_vectors[setv_fill_count++] = 0;
  2637.         }
  2638.       if (sp->defined)
  2639.         defined_global_sym_count++;
  2640.     }
  2641.     }
  2642.  
  2643.   if (end_symbol)        /* These are null if -r.  */
  2644.     {
  2645.       etext_symbol->value = text_size + text_start;
  2646.       edata_symbol->value = data_start + data_size;
  2647.       end_symbol->value = data_start + data_size + bss_size;
  2648.     }
  2649.  
  2650.   /* Figure the data_pad now, so that it overlaps with the bss addresses.  */
  2651.  
  2652.   if (specified_data_size && specified_data_size > data_size)
  2653.     data_pad = specified_data_size - data_size;
  2654.  
  2655.   if (magic == ZMAGIC)
  2656.     data_pad = ((data_pad + data_size + page_size - 1) & (- page_size))
  2657.                - data_size;
  2658.  
  2659.   bss_size -= data_pad;
  2660.   if (bss_size < 0) bss_size = 0;
  2661.  
  2662.   data_size += data_pad;
  2663. }
  2664.  
  2665. /* Accumulate the section sizes of input file ENTRY
  2666.    into the section sizes of the output file.  */
  2667.  
  2668. void
  2669. consider_file_section_lengths (entry)
  2670.      register struct file_entry *entry;
  2671. {
  2672.   if (entry->just_syms_flag)
  2673.     return;
  2674.  
  2675.   entry->text_start_address = text_size;
  2676.   /* If there were any vectors, we need to chop them off */
  2677.   text_size += entry->header.a_text;
  2678.   entry->data_start_address = data_size;
  2679.   data_size += entry->header.a_data;
  2680.   entry->bss_start_address = bss_size;
  2681.   bss_size += entry->header.a_bss;
  2682.  
  2683.   text_reloc_size += entry->header.a_trsize;
  2684.   data_reloc_size += entry->header.a_drsize;
  2685. }
  2686.  
  2687. /* Determine where the sections of ENTRY go into the output file,
  2688.    whose total section sizes are already known.
  2689.    Also relocate the addresses of the file's local and debugger symbols.  */
  2690.  
  2691. void
  2692. relocate_file_addresses (entry)
  2693.      register struct file_entry *entry;
  2694. {
  2695.   entry->text_start_address += text_start;
  2696.   /* Note that `data_start' and `data_size' have not yet been
  2697.      adjusted for `data_pad'.  If they had been, we would get the wrong
  2698.      results here.  */
  2699.   entry->data_start_address += data_start;
  2700.   entry->bss_start_address += data_start + data_size;
  2701.  
  2702.   {
  2703.     register struct nlist *p;
  2704.     register struct nlist *end
  2705.       = entry->symbols + entry->header.a_syms / sizeof (struct nlist);
  2706.  
  2707.     for (p = entry->symbols; p < end; p++)
  2708.       {
  2709.     /* If this belongs to a section, update it by the section's start address */
  2710.     register int type = p->n_type & N_TYPE;
  2711.  
  2712.     switch (type)
  2713.       {
  2714.       case N_TEXT:
  2715.       case N_SETT:
  2716.         p->n_value += entry->text_start_address;
  2717.         break;
  2718.       case N_DATA:
  2719.       case N_SETV:
  2720.       case N_SETD:
  2721.         /* A symbol whose value is in the data section
  2722.            is present in the input file as if the data section
  2723.            started at an address equal to the length of the file's text.  */
  2724.         p->n_value += entry->data_start_address - entry->header.a_text;
  2725.         break;
  2726.       case N_BSS:
  2727.       case N_SETB:
  2728.         /* likewise for symbols with value in BSS.  */
  2729.         p->n_value += entry->bss_start_address
  2730.           - entry->header.a_text - entry->header.a_data;
  2731.         break;
  2732.       }
  2733.       }
  2734.   }
  2735. }
  2736.  
  2737. void describe_file_sections (), list_file_locals ();
  2738.  
  2739. /* Print a complete or partial map of the output file.  */
  2740.  
  2741. void
  2742. print_symbols (outfile)
  2743.      FILE *outfile;
  2744. {
  2745.   register int i;
  2746.  
  2747.   fprintf (outfile, "\nFiles:\n\n");
  2748.  
  2749.   each_file (describe_file_sections, outfile);
  2750.  
  2751.   fprintf (outfile, "\nGlobal symbols:\n\n");
  2752.  
  2753.   for (i = 0; i < TABSIZE; i++)
  2754.     {
  2755.       register symbol *sp;
  2756.       for (sp = symtab[i]; sp; sp = sp->link)
  2757.     {
  2758.       if (sp->defined == 1)
  2759.         fprintf (outfile, "  %s: common, length 0x%x\n", sp->name, sp->max_common_size);
  2760.       if (sp->defined)
  2761.         fprintf (outfile, "  %s: 0x%x\n", sp->name, sp->value);
  2762.       else if (sp->referenced)
  2763.         fprintf (outfile, "  %s: undefined\n", sp->name);
  2764.     }
  2765.     }
  2766.  
  2767.   each_file (list_file_locals, outfile);
  2768. }
  2769.  
  2770. void
  2771. describe_file_sections (entry, outfile)
  2772.      struct file_entry *entry;
  2773.      FILE *outfile;
  2774. {
  2775.   fprintf (outfile, "  ");
  2776.   print_file_name (entry, outfile);
  2777.   if (entry->just_syms_flag)
  2778.     fprintf (outfile, " symbols only\n", 0);
  2779.   else
  2780.     fprintf (outfile, " text %x(%x), data %x(%x), bss %x(%x) hex\n",
  2781.          entry->text_start_address, entry->header.a_text,
  2782.          entry->data_start_address, entry->header.a_data,
  2783.          entry->bss_start_address, entry->header.a_bss);
  2784. }
  2785.  
  2786. void
  2787. list_file_locals (entry, outfile)
  2788.      struct file_entry *entry;
  2789.      FILE *outfile;
  2790. {
  2791.   register struct nlist
  2792.     *p,
  2793.     *end = entry->symbols + entry->header.a_syms / sizeof (struct nlist);
  2794.  
  2795.   entry->strings = (char *) alloca (entry->string_size);
  2796.   read_entry_strings (file_open (entry), entry);
  2797.  
  2798.   fprintf (outfile, "\nLocal symbols of ");
  2799.   print_file_name (entry, outfile);
  2800.   fprintf (outfile, ":\n\n");
  2801.  
  2802.   for (p = entry->symbols; p < end; p++)
  2803.     /* If this is a definition,
  2804.        update it if necessary by this file's start address.  */
  2805.     if (!(p->n_type & (N_STAB | N_EXT)))
  2806.       fprintf (outfile, "  %s: 0x%x\n",
  2807.            entry->strings + p->n_un.n_strx, p->n_value);
  2808.  
  2809.   entry->strings = 0;        /* All done with them.  */
  2810. }
  2811.  
  2812.  
  2813. /* Static vars for do_warnings and subroutines of it */
  2814. int list_unresolved_refs;    /* List unresolved refs */
  2815. int list_warning_symbols;    /* List warning syms */
  2816. int list_multiple_defs;        /* List multiple definitions */
  2817.  
  2818. /*
  2819.  * Structure for communication between do_file_warnings and it's
  2820.  * helper routines.  Will in practice be an array of three of these:
  2821.  * 0) Current line, 1) Next line, 2) Source file info.
  2822.  */
  2823. struct line_debug_entry
  2824. {
  2825.   int line;
  2826.   char *filename;
  2827.   struct nlist *sym;
  2828. };
  2829.  
  2830. void qsort ();
  2831. /*
  2832.  * Helper routines for do_file_warnings.
  2833.  */
  2834.  
  2835. /* Return an integer less than, equal to, or greater than 0 as per the
  2836.    relation between the two relocation entries.  Used by qsort.  */
  2837.  
  2838. int
  2839. relocation_entries_relation (rel1, rel2)
  2840.      struct relocation_info *rel1, *rel2;
  2841. {
  2842.   return RELOC_ADDRESS(rel1) - RELOC_ADDRESS(rel2);
  2843. }
  2844.  
  2845. /* Moves to the next debugging symbol in the file.  USE_DATA_SYMBOLS
  2846.    determines the type of the debugging symbol to look for (DSLINE or
  2847.    SLINE).  STATE_POINTER keeps track of the old and new locatiosn in
  2848.    the file.  It assumes that state_pointer[1] is valid; ie
  2849.    that it.sym points into some entry in the symbol table.  If
  2850.    state_pointer[1].sym == 0, this routine should not be called.  */
  2851.  
  2852. int
  2853. next_debug_entry (use_data_symbols, state_pointer)
  2854.      register int use_data_symbols;
  2855.      /* Next must be passed by reference! */
  2856.      struct line_debug_entry state_pointer[3];
  2857. {
  2858.   register struct line_debug_entry
  2859.     *current = state_pointer,
  2860.     *next = state_pointer + 1,
  2861.     /* Used to store source file */
  2862.     *source = state_pointer + 2;
  2863.   struct file_entry *entry = (struct file_entry *) source->sym;
  2864.  
  2865.   current->sym = next->sym;
  2866.   current->line = next->line;
  2867.   current->filename = next->filename;
  2868.  
  2869.   while (++(next->sym) < (entry->symbols
  2870.               + entry->header.a_syms/sizeof (struct nlist)))
  2871.     {
  2872.       /* n_type is a char, and N_SOL, N_EINCL and N_BINCL are > 0x80, so
  2873.        * may look negative...therefore, must mask to low bits
  2874.        */
  2875.       switch (next->sym->n_type & 0xff)
  2876.     {
  2877.     case N_SLINE:
  2878.       if (use_data_symbols) continue;
  2879.       next->line = next->sym->n_desc;
  2880.       return 1;
  2881.     case N_DSLINE:
  2882.       if (!use_data_symbols) continue;
  2883.       next->line = next->sym->n_desc;
  2884.       return 1;
  2885. #ifdef HAVE_SUN_STABS
  2886.     case N_EINCL:
  2887.       next->filename = source->filename;
  2888.       continue;
  2889. #endif
  2890.     case N_SO:
  2891.       source->filename = next->sym->n_un.n_strx + entry->strings;
  2892.       source->line++;
  2893. #ifdef HAVE_SUN_STABS
  2894.     case N_BINCL:
  2895. #endif
  2896.     case N_SOL:
  2897.       next->filename
  2898.         = next->sym->n_un.n_strx + entry->strings;
  2899.     default:
  2900.       continue;
  2901.     }
  2902.     }
  2903.   next->sym = (struct nlist *) 0;
  2904.   return 0;
  2905. }
  2906.  
  2907. /* Create a structure to save the state of a scan through the debug
  2908.    symbols.  USE_DATA_SYMBOLS is set if we should be scanning for
  2909.    DSLINE's instead of SLINE's.  entry is the file entry which points
  2910.    at the symbols to use.  */
  2911.  
  2912. struct line_debug_entry *
  2913. init_debug_scan (use_data_symbols, entry)
  2914.      int use_data_symbols;
  2915.      struct file_entry *entry;
  2916. {
  2917.   struct line_debug_entry
  2918.     *state_pointer
  2919.       = (struct line_debug_entry *)
  2920.     xmalloc (3 * sizeof (struct line_debug_entry));
  2921.   register struct line_debug_entry
  2922.     *current = state_pointer,
  2923.     *next = state_pointer + 1,
  2924.     *source = state_pointer + 2; /* Used to store source file */
  2925.  
  2926.   struct nlist *tmp;
  2927.  
  2928.   for (tmp = entry->symbols;
  2929.        tmp < (entry->symbols
  2930.           + entry->header.a_syms/sizeof (struct nlist));
  2931.        tmp++)
  2932.     if (tmp->n_type == (int) N_SO)
  2933.       break;
  2934.  
  2935.   if (tmp >= (entry->symbols
  2936.           + entry->header.a_syms/sizeof (struct nlist)))
  2937.     {
  2938.       /* I believe this translates to "We lose" */
  2939.       current->filename = next->filename = entry->filename;
  2940.       current->line = next->line = -1;
  2941.       current->sym = next->sym = (struct nlist *) 0;
  2942.       return state_pointer;
  2943.     }
  2944.  
  2945.   next->line = source->line = 0;
  2946.   next->filename = source->filename
  2947.     = (tmp->n_un.n_strx + entry->strings);
  2948.   source->sym = (struct nlist *) entry;
  2949.   next->sym = tmp;
  2950.  
  2951.   next_debug_entry (use_data_symbols, state_pointer); /* To setup next */
  2952.  
  2953.   if (!next->sym)        /* No line numbers for this section; */
  2954.                 /* setup output results as appropriate */
  2955.     {
  2956.       if (source->line)
  2957.     {
  2958.       current->filename = source->filename = entry->filename;
  2959.       current->line = -1;    /* Don't print lineno */
  2960.     }
  2961.       else
  2962.     {
  2963.       current->filename = source->filename;
  2964.       current->line = 0;
  2965.     }
  2966.       return state_pointer;
  2967.     }
  2968.  
  2969.  
  2970.   next_debug_entry (use_data_symbols, state_pointer); /* To setup current */
  2971.  
  2972.   return state_pointer;
  2973. }
  2974.  
  2975. /* Takes an ADDRESS (in either text or data space) and a STATE_POINTER
  2976.    which describes the current location in the implied scan through
  2977.    the debug symbols within the file which ADDRESS is within, and
  2978.    returns the source line number which corresponds to ADDRESS.  */
  2979.  
  2980. int
  2981. address_to_line (address, state_pointer)
  2982.      unsigned long address;
  2983.      /* Next must be passed by reference! */
  2984.      struct line_debug_entry state_pointer[3];
  2985. {
  2986.   struct line_debug_entry
  2987.     *current = state_pointer,
  2988.     *next = state_pointer + 1;
  2989.   struct line_debug_entry *tmp_pointer;
  2990.  
  2991.   int use_data_symbols;
  2992.  
  2993.   if (next->sym)
  2994.     use_data_symbols = (next->sym->n_type & N_TYPE) == N_DATA;
  2995.   else
  2996.     return current->line;
  2997.  
  2998.   /* Go back to the beginning if we've already passed it.  */
  2999.   if (current->sym->n_value > address)
  3000.     {
  3001.       tmp_pointer = init_debug_scan (use_data_symbols,
  3002.                      (struct file_entry *)
  3003.                      ((state_pointer + 2)->sym));
  3004.       state_pointer[0] = tmp_pointer[0];
  3005.       state_pointer[1] = tmp_pointer[1];
  3006.       state_pointer[2] = tmp_pointer[2];
  3007.       free (tmp_pointer);
  3008.     }
  3009.  
  3010.   /* If we're still in a bad way, return -1, meaning invalid line.  */
  3011.   if (current->sym->n_value > address)
  3012.     return -1;
  3013.  
  3014.   while (next->sym
  3015.      && next->sym->n_value <= address
  3016.      && next_debug_entry (use_data_symbols, state_pointer))
  3017.     ;
  3018.   return current->line;
  3019. }
  3020.  
  3021.  
  3022. /* Macros for manipulating bitvectors.  */
  3023. #define    BIT_SET_P(bv, index)    ((bv)[(index) >> 3] & 1 << ((index) & 0x7))
  3024. #define    SET_BIT(bv, index)    ((bv)[(index) >> 3] |= 1 << ((index) & 0x7))
  3025.  
  3026. /* This routine will scan through the relocation data of file ENTRY,
  3027.    printing out references to undefined symbols and references to
  3028.    symbols defined in files with N_WARNING symbols.  If DATA_SEGMENT
  3029.    is non-zero, it will scan the data relocation segment (and use
  3030.    N_DSLINE symbols to track line number); otherwise it will scan the
  3031.    text relocation segment.  Warnings will be printed on the output
  3032.    stream OUTFILE.  Eventually, every nlist symbol mapped through will
  3033.    be marked in the NLIST_BITVECTOR, so we don't repeat ourselves when
  3034.    we scan the nlists themselves.  */
  3035.  
  3036. do_relocation_warnings (entry, data_segment, outfile, nlist_bitvector)
  3037.      struct file_entry *entry;
  3038.      int data_segment;
  3039.      FILE *outfile;
  3040.      unsigned char *nlist_bitvector;
  3041. {
  3042.   struct relocation_info
  3043.     *reloc_start = data_segment ? entry->datarel : entry->textrel,
  3044.     *reloc;
  3045.   int reloc_size
  3046.     = ((data_segment ? entry->header.a_drsize : entry->header.a_trsize)
  3047.        / sizeof (struct relocation_info));
  3048.   int start_of_segment
  3049.     = (data_segment ? entry->data_start_address : entry->text_start_address);
  3050.   struct nlist *start_of_syms = entry->symbols;
  3051.   struct line_debug_entry *state_pointer
  3052.     = init_debug_scan (data_segment != 0, entry);
  3053.   register struct line_debug_entry
  3054.     *current = state_pointer;
  3055.   /* Assigned to generally static values; should not be written into.  */
  3056.   char *errfmt;
  3057.   /* Assigned to alloca'd values cand copied into; should be freed
  3058.      when done.  */
  3059.   char *errmsg;
  3060.   int invalidate_line_number;
  3061.  
  3062.   /* We need to sort the relocation info here.  Sheesh, so much effort
  3063.      for one lousy error optimization. */
  3064.  
  3065.   qsort (reloc_start, reloc_size, sizeof (struct relocation_info),
  3066.      relocation_entries_relation);
  3067.  
  3068.   for (reloc = reloc_start;
  3069.        reloc < (reloc_start + reloc_size);
  3070.        reloc++)
  3071.     {
  3072.       register struct nlist *s;
  3073.       register symbol *g;
  3074.  
  3075.       /* If the relocation isn't resolved through a symbol, continue */
  3076.       if (!RELOC_EXTERN_P(reloc))
  3077.     continue;
  3078.  
  3079.       s = &(entry->symbols[RELOC_SYMBOL(reloc)]);
  3080.  
  3081.       /* Local symbols shouldn't ever be used by relocation info, so
  3082.      the next should be safe.
  3083.      This is, of course, wrong.  References to local BSS symbols can be
  3084.      the targets of relocation info, and they can (must) be
  3085.      resolved through symbols.  However, these must be defined properly,
  3086.      (the assembler would have caught it otherwise), so we can
  3087.      ignore these cases.  */
  3088.       if (!(s->n_type & N_EXT))
  3089.     continue;
  3090.  
  3091.       g = (symbol *) s->n_un.n_name;
  3092.       errmsg = 0;
  3093.  
  3094.       if (!g->defined && list_unresolved_refs) /* Reference */
  3095.     {
  3096.       /* Mark as being noted by relocation warning pass.  */
  3097.       SET_BIT (nlist_bitvector, s - start_of_syms);
  3098.  
  3099.       if (g->undef_refs >= MAX_UREFS_PRINTED)    /* Listed too many */
  3100.         continue;
  3101.  
  3102.       /* Undefined symbol which we should mention */
  3103.  
  3104.       if (++(g->undef_refs) == MAX_UREFS_PRINTED)
  3105.         {
  3106.           errfmt = "More undefined symbol %s refs follow";
  3107.           invalidate_line_number = 1;
  3108.         }
  3109.       else
  3110.         {
  3111.           errfmt = "Undefined symbol %s referenced from %s segment";
  3112.           invalidate_line_number = 0;
  3113.         }
  3114.     }
  3115.       else                         /* Defined */
  3116.     {
  3117.       /* Potential symbol warning here */
  3118.       if (!g->warning) continue;
  3119.  
  3120.       /* Mark as being noted by relocation warning pass.  */
  3121.       SET_BIT (nlist_bitvector, s - start_of_syms);
  3122.  
  3123.       errfmt = 0;
  3124.       errmsg = g->warning;
  3125.       invalidate_line_number = 0;
  3126.     }
  3127.  
  3128.  
  3129.       /* If errfmt == 0, errmsg has already been defined.  */
  3130.       if (errfmt != 0)
  3131.     {
  3132.       char *nm;
  3133.  
  3134.       if (demangler == NULL || (nm = (*demangler)(g->name)) == NULL)
  3135.         nm = g->name;
  3136.       errmsg = (char *) xmalloc (strlen (errfmt) + strlen (nm) + 1);
  3137.       sprintf (errmsg, errfmt, nm, data_segment ? "data" : "text");
  3138.       if (nm != g->name)
  3139.         free (nm);
  3140.     }
  3141.  
  3142.       address_to_line (RELOC_ADDRESS (reloc) + start_of_segment,
  3143.                state_pointer);
  3144.  
  3145.       if (current->line >=0)
  3146.     fprintf (outfile, "%s:%d: %s\n", current->filename,
  3147.          invalidate_line_number ? 0 : current->line, errmsg);
  3148.       else
  3149.     fprintf (outfile, "%s: %s\n", current->filename, errmsg);
  3150.  
  3151.       if (errfmt != 0)
  3152.     free (errmsg);
  3153.     }
  3154.  
  3155.   free (state_pointer);
  3156. }
  3157.  
  3158. /* Print on OUTFILE a list of all warnings generated by references
  3159.    and/or definitions in the file ENTRY.  List source file and line
  3160.    number if possible, just the .o file if not. */
  3161.  
  3162. void
  3163. do_file_warnings (entry, outfile)
  3164.      struct file_entry *entry;
  3165.      FILE *outfile;
  3166. {
  3167.   int number_of_syms = entry->header.a_syms / sizeof (struct nlist);
  3168.   unsigned char *nlist_bitvector
  3169.     = (unsigned char *) alloca ((number_of_syms >> 3) + 1);
  3170.   struct line_debug_entry *text_scan, *data_scan;
  3171.   int i;
  3172.   char *errfmt, *file_name;
  3173.   int line_number;
  3174.   int dont_allow_symbol_name;
  3175.  
  3176.   bzero (nlist_bitvector, (number_of_syms >> 3) + 1);
  3177.  
  3178.   /* Read in the files strings if they aren't available */
  3179.   if (!entry->strings)
  3180.     {
  3181.       int desc;
  3182.  
  3183.       entry->strings = (char *) alloca (entry->string_size);
  3184.       desc = file_open (entry);
  3185.       read_entry_strings (desc, entry);
  3186.     }
  3187.  
  3188.   read_file_relocation (entry);
  3189.  
  3190.   /* Do text warnings based on a scan through the relocation info.  */
  3191.   do_relocation_warnings (entry, 0, outfile, nlist_bitvector);
  3192.  
  3193.   /* Do data warnings based on a scan through the relocation info.  */
  3194.   do_relocation_warnings (entry, 1, outfile, nlist_bitvector);
  3195.  
  3196.   /* Scan through all of the nlist entries in this file and pick up
  3197.      anything that the scan through the relocation stuff didn't.  */
  3198.  
  3199.   text_scan = init_debug_scan (0, entry);
  3200.   data_scan = init_debug_scan (1, entry);
  3201.  
  3202.   for (i = 0; i < number_of_syms; i++)
  3203.     {
  3204.       struct nlist *s;
  3205.       struct glosym *g;
  3206.  
  3207.       s = entry->symbols + i;
  3208.  
  3209.       if (!(s->n_type & N_EXT))
  3210.     continue;
  3211.  
  3212.       g = (symbol *) s->n_un.n_name;
  3213.       dont_allow_symbol_name = 0;
  3214.  
  3215.       if (list_multiple_defs && g->multiply_defined)
  3216.     {
  3217.       errfmt = "Definition of symbol %s (multiply defined)";
  3218.       switch (s->n_type)
  3219.         {
  3220.         case N_TEXT | N_EXT:
  3221.           line_number = address_to_line (s->n_value, text_scan);
  3222.           file_name = text_scan[0].filename;
  3223.           break;
  3224.         case N_DATA | N_EXT:
  3225.           line_number = address_to_line (s->n_value, data_scan);
  3226.           file_name = data_scan[0].filename;
  3227.           break;
  3228.         case N_SETA | N_EXT:
  3229.         case N_SETT | N_EXT:
  3230.         case N_SETD | N_EXT:
  3231.         case N_SETB | N_EXT:
  3232.           if (g->multiply_defined == 2)
  3233.         continue;
  3234.           errfmt = "First set element definition of symbol %s (multiply defined)";
  3235.           break;
  3236.         default:
  3237.           continue;        /* Don't print out multiple defs
  3238.                    at references.  */
  3239.         }
  3240.     }
  3241.       else if (BIT_SET_P (nlist_bitvector, i))
  3242.     continue;
  3243.       else if (list_unresolved_refs && !g->defined)
  3244.     {
  3245.       if (g->undef_refs >= MAX_UREFS_PRINTED)
  3246.         continue;
  3247.  
  3248.       if (++(g->undef_refs) == MAX_UREFS_PRINTED)
  3249.         errfmt = "More undefined \"%s\" refs follow";
  3250.       else
  3251.         errfmt = "Undefined symbol \"%s\" referenced";
  3252.       line_number = -1;
  3253.     }
  3254.       else if (g->warning)
  3255.     {
  3256.       /* There are two cases in which we don't want to
  3257.          do this.  The first is if this is a definition instead of
  3258.          a reference.  The second is if it's the reference used by
  3259.          the warning stabs itself.  */
  3260.       if (s->n_type != (N_EXT | N_UNDF)
  3261.           || (i && (s-1)->n_type == N_WARNING))
  3262.         continue;
  3263.  
  3264.       errfmt = g->warning;
  3265.       line_number = -1;
  3266.       dont_allow_symbol_name = 1;
  3267.     }
  3268.       else
  3269.     continue;
  3270.  
  3271.       if (line_number == -1)
  3272.     fprintf (outfile, "%s: ", entry->filename);
  3273.       else
  3274.     fprintf (outfile, "%s:%d: ", file_name, line_number);
  3275.  
  3276.       if (dont_allow_symbol_name)
  3277.     fprintf (outfile, "%s", errfmt);
  3278.       else
  3279.     {
  3280.       char *nm;
  3281.       if (demangler != NULL && (nm = (*demangler)(g->name)) != NULL)
  3282.         {
  3283.           fprintf (outfile, errfmt, nm);
  3284.           free (nm);
  3285.         }
  3286.       else
  3287.         fprintf (outfile, errfmt, g->name);
  3288.     }
  3289.  
  3290.       fputc ('\n', outfile);
  3291.     }
  3292.   free (text_scan);
  3293.   free (data_scan);
  3294.   entry->strings = 0;        /* Since it will dissapear anyway.  */
  3295. }
  3296.  
  3297. do_warnings (outfile)
  3298.      FILE *outfile;
  3299. {
  3300.   list_unresolved_refs = !relocatable_output && undefined_global_sym_count;
  3301.   list_warning_symbols = warning_count;
  3302.   list_multiple_defs = multiple_def_count != 0;
  3303.  
  3304.   if (!(list_unresolved_refs ||
  3305.     list_warning_symbols ||
  3306.     list_multiple_defs      ))
  3307.     /* No need to run this routine */
  3308.     return;
  3309.  
  3310.   each_file (do_file_warnings, outfile);
  3311.  
  3312.   if (list_unresolved_refs || list_multiple_defs)
  3313.     make_executable = 0;
  3314. }
  3315.  
  3316. /* Write the output file */
  3317.  
  3318. void
  3319. write_output ()
  3320. {
  3321.   struct stat statbuf;
  3322.   int filemode;
  3323.  
  3324.   (void) unlink (output_filename);
  3325.   outdesc = open (output_filename, O_WRONLY | O_CREAT | O_TRUNC, 0666);
  3326.   if (outdesc < 0) perror_name (output_filename);
  3327.  
  3328.   if (fstat (outdesc, &statbuf) < 0)
  3329.     perror_name (output_filename);
  3330.  
  3331.   filemode = statbuf.st_mode;
  3332.  
  3333.   chmod (output_filename, filemode & ~0111);
  3334.  
  3335.   /* Output the a.out header.  */
  3336.   write_header ();
  3337.  
  3338.   /* Output the text and data segments, relocating as we go.  */
  3339.   write_text ();
  3340.   write_data ();
  3341.  
  3342.   /* Output the merged relocation info, if requested with `-r'.  */
  3343.   if (relocatable_output)
  3344.     write_rel ();
  3345.  
  3346.   /* Output the symbol table (both globals and locals).  */
  3347.   write_syms ();
  3348.  
  3349.   /* Copy any GDB symbol segments from input files.  */
  3350.   write_symsegs ();
  3351.  
  3352.   close (outdesc);
  3353.  
  3354.   if (chmod (output_filename, filemode | 0111) == -1)
  3355.     perror_name (output_filename);
  3356. }
  3357.  
  3358. void modify_location (), perform_relocation (), copy_text (), copy_data ();
  3359.  
  3360. void
  3361. write_header ()
  3362. {
  3363.   N_SET_MAGIC (outheader, magic);
  3364.   outheader.a_text = text_size;
  3365. #ifdef sequent
  3366.   outheader.a_text += N_ADDRADJ (outheader);
  3367.   if (entry_symbol == 0)
  3368.     entry_symbol = getsym("start");
  3369. #endif
  3370.   outheader.a_data = data_size;
  3371.   outheader.a_bss = bss_size;
  3372.   outheader.a_entry = (entry_symbol ? entry_symbol->value
  3373.                : text_start + entry_offset);
  3374. #ifdef COFF_ENCAPSULATE
  3375.   if (need_coff_header)
  3376.     {
  3377.       /* We are encapsulating BSD format within COFF format.  */
  3378.       struct coffscn *tp, *dp, *bp;
  3379.  
  3380.       tp = &coffheader.scns[0];
  3381.       dp = &coffheader.scns[1];
  3382.       bp = &coffheader.scns[2];
  3383.  
  3384.       strcpy (tp->s_name, ".text");
  3385.       tp->s_paddr = text_start;
  3386.       tp->s_vaddr = text_start;
  3387.       tp->s_size = text_size;
  3388.       tp->s_scnptr = sizeof (struct coffheader) + sizeof (struct exec);
  3389.       tp->s_relptr = 0;
  3390.       tp->s_lnnoptr = 0;
  3391.       tp->s_nreloc = 0;
  3392.       tp->s_nlnno = 0;
  3393.       tp->s_flags = 0x20;
  3394.       strcpy (dp->s_name, ".data");
  3395.       dp->s_paddr = data_start;
  3396.       dp->s_vaddr = data_start;
  3397.       dp->s_size = data_size;
  3398.       dp->s_scnptr = tp->s_scnptr + tp->s_size;
  3399.       dp->s_relptr = 0;
  3400.       dp->s_lnnoptr = 0;
  3401.       dp->s_nreloc = 0;
  3402.       dp->s_nlnno = 0;
  3403.       dp->s_flags = 0x40;
  3404.       strcpy (bp->s_name, ".bss");
  3405.       bp->s_paddr = dp->s_vaddr + dp->s_size;
  3406.       bp->s_vaddr = bp->s_paddr;
  3407.       bp->s_size = bss_size;
  3408.       bp->s_scnptr = 0;
  3409.       bp->s_relptr = 0;
  3410.       bp->s_lnnoptr = 0;
  3411.       bp->s_nreloc = 0;
  3412.       bp->s_nlnno = 0;
  3413.       bp->s_flags = 0x80;
  3414.  
  3415.       coffheader.f_magic = COFF_MAGIC;
  3416.       coffheader.f_nscns = 3;
  3417.       /* store an unlikely time so programs can
  3418.        * tell that there is a bsd header
  3419.        */
  3420.       coffheader.f_timdat = 1;
  3421.       coffheader.f_symptr = 0;
  3422.       coffheader.f_nsyms = 0;
  3423.       coffheader.f_opthdr = 28;
  3424.       coffheader.f_flags = 0x103;
  3425.       /* aouthdr */
  3426.       coffheader.magic = ZMAGIC;
  3427.       coffheader.vstamp = 0;
  3428.       coffheader.tsize = tp->s_size;
  3429.       coffheader.dsize = dp->s_size;
  3430.       coffheader.bsize = bp->s_size;
  3431.       coffheader.entry = outheader.a_entry;
  3432.       coffheader.text_start = tp->s_vaddr;
  3433.       coffheader.data_start = dp->s_vaddr;
  3434.     }
  3435. #endif
  3436.  
  3437. #ifdef INITIALIZE_HEADER
  3438.   INITIALIZE_HEADER;
  3439. #endif
  3440.  
  3441.   if (strip_symbols == STRIP_ALL)
  3442.     nsyms = 0;
  3443.   else
  3444.     {
  3445.       nsyms = (defined_global_sym_count
  3446.            + undefined_global_sym_count);
  3447.       if (discard_locals == DISCARD_L)
  3448.     nsyms += non_L_local_sym_count;
  3449.       else if (discard_locals == DISCARD_NONE)
  3450.     nsyms += local_sym_count;
  3451.       /* One extra for following reference on indirects */
  3452.       if (relocatable_output)
  3453.     nsyms += set_symbol_count + global_indirect_count;
  3454.     }
  3455.  
  3456.   if (strip_symbols == STRIP_NONE)
  3457.     nsyms += debugger_sym_count;
  3458.  
  3459.   outheader.a_syms = nsyms * sizeof (struct nlist);
  3460.  
  3461.   if (relocatable_output)
  3462.     {
  3463.       outheader.a_trsize = text_reloc_size;
  3464.       outheader.a_drsize = data_reloc_size;
  3465.     }
  3466.   else
  3467.     {
  3468.       outheader.a_trsize = 0;
  3469.       outheader.a_drsize = 0;
  3470.     }
  3471.  
  3472. #ifdef COFF_ENCAPSULATE
  3473.   if (need_coff_header)
  3474.     mywrite (&coffheader, sizeof coffheader, 1, outdesc);
  3475. #endif
  3476.   mywrite (&outheader, sizeof (struct exec), 1, outdesc);
  3477.  
  3478.   /* Output whatever padding is required in the executable file
  3479.      between the header and the start of the text.  */
  3480.  
  3481. #ifndef COFF_ENCAPSULATE
  3482.   padfile (N_TXTOFF (outheader) - sizeof outheader, outdesc);
  3483. #endif
  3484. }
  3485.  
  3486. /* Relocate the text segment of each input file
  3487.    and write to the output file.  */
  3488.  
  3489. void
  3490. write_text ()
  3491. {
  3492.   if (trace_files)
  3493.     fprintf (stderr, "Copying and relocating text:\n\n");
  3494.  
  3495.   each_full_file (copy_text, 0);
  3496.   file_close ();
  3497.  
  3498.   if (trace_files)
  3499.     fprintf (stderr, "\n");
  3500.  
  3501.   padfile (text_pad, outdesc);
  3502. }
  3503.  
  3504. int
  3505. text_offset (entry)
  3506.      struct file_entry *entry;
  3507. {
  3508.   return entry->starting_offset + N_TXTOFF (entry->header);
  3509. }
  3510.  
  3511. /* Read in all of the relocation information */
  3512.  
  3513. void
  3514. read_relocation ()
  3515. {
  3516.   each_full_file (read_file_relocation, 0);
  3517. }
  3518.  
  3519. /* Read in the relocation sections of ENTRY if necessary */
  3520.  
  3521. void
  3522. read_file_relocation (entry)
  3523.      struct file_entry *entry;
  3524. {
  3525.   register struct relocation_info *reloc;
  3526.   int desc;
  3527.   int read_return;
  3528.  
  3529.   desc = -1;
  3530.   if (!entry->textrel)
  3531.     {
  3532.       reloc = (struct relocation_info *) xmalloc (entry->header.a_trsize);
  3533.       desc = file_open (entry);
  3534.       lseek (desc,
  3535.          text_offset (entry) + entry->header.a_text + entry->header.a_data,
  3536.          L_SET);
  3537.       if (entry->header.a_trsize != (read_return = read (desc, reloc, entry->header.a_trsize)))
  3538.     {
  3539.       fprintf (stderr, "Return from read: %d\n", read_return);
  3540.       fatal_with_file ("premature eof in text relocation of ", entry);
  3541.     }
  3542.       entry->textrel = reloc;
  3543.     }
  3544.  
  3545.   if (!entry->datarel)
  3546.     {
  3547.       reloc = (struct relocation_info *) xmalloc (entry->header.a_drsize);
  3548.       if (desc == -1) desc = file_open (entry);
  3549.       lseek (desc,
  3550.          text_offset (entry) + entry->header.a_text
  3551.          + entry->header.a_data + entry->header.a_trsize,
  3552.          L_SET);
  3553.       if (entry->header.a_drsize != read (desc, reloc, entry->header.a_drsize))
  3554.     fatal_with_file ("premature eof in data relocation of ", entry);
  3555.       entry->datarel = reloc;
  3556.     }
  3557. }
  3558.  
  3559. /* Read the text segment contents of ENTRY, relocate them,
  3560.    and write the result to the output file.
  3561.    If `-r', save the text relocation for later reuse.  */
  3562.  
  3563. void
  3564. copy_text (entry)
  3565.      struct file_entry *entry;
  3566. {
  3567.   register char *bytes;
  3568.   register int desc;
  3569.   register struct relocation_info *reloc;
  3570.  
  3571.   if (trace_files)
  3572.     prline_file_name (entry, stderr);
  3573.  
  3574.   desc = file_open (entry);
  3575.  
  3576.   /* Allocate space for the file's text section */
  3577.  
  3578.   bytes = (char *) alloca (entry->header.a_text);
  3579.  
  3580.   /* Deal with relocation information however is appropriate */
  3581.  
  3582.   if (entry->textrel)  reloc = entry->textrel;
  3583.   else if (relocatable_output)
  3584.     {
  3585.       read_file_relocation (entry);
  3586.       reloc = entry->textrel;
  3587.     }
  3588.   else
  3589.     {
  3590.       reloc = (struct relocation_info *) alloca (entry->header.a_trsize);
  3591.       lseek (desc, text_offset (entry) + entry->header.a_text + entry->header.a_data, 0);
  3592.       if (entry->header.a_trsize != read (desc, reloc, entry->header.a_trsize))
  3593.     fatal_with_file ("premature eof in text relocation of ", entry);
  3594.     }
  3595.  
  3596.   /* Read the text section into core.  */
  3597.  
  3598.   lseek (desc, text_offset (entry), 0);
  3599.   if (entry->header.a_text != read (desc, bytes, entry->header.a_text))
  3600.     fatal_with_file ("premature eof in text section of ", entry);
  3601.  
  3602.  
  3603.   /* Relocate the text according to the text relocation.  */
  3604.  
  3605.   perform_relocation (bytes, entry->text_start_address, entry->header.a_text,
  3606.               reloc, entry->header.a_trsize, entry);
  3607.  
  3608.   /* Write the relocated text to the output file.  */
  3609.  
  3610.   mywrite (bytes, 1, entry->header.a_text, outdesc);
  3611. }
  3612.  
  3613. /* Relocate the data segment of each input file
  3614.    and write to the output file.  */
  3615.  
  3616. void
  3617. write_data ()
  3618. {
  3619.   if (trace_files)
  3620.     fprintf (stderr, "Copying and relocating data:\n\n");
  3621.  
  3622.   each_full_file (copy_data, 0);
  3623.   file_close ();
  3624.  
  3625.   /* Write out the set element vectors.  See digest symbols for
  3626.      description of length of the set vector section.  */
  3627.  
  3628.   if (set_vector_count)
  3629.     mywrite (set_vectors, 2 * set_symbol_count + set_vector_count,
  3630.          sizeof (unsigned long), outdesc);
  3631.  
  3632.   if (trace_files)
  3633.     fprintf (stderr, "\n");
  3634.  
  3635.   padfile (data_pad, outdesc);
  3636. }
  3637.  
  3638. /* Read the data segment contents of ENTRY, relocate them,
  3639.    and write the result to the output file.
  3640.    If `-r', save the data relocation for later reuse.
  3641.    See comments in `copy_text'.  */
  3642.  
  3643. void
  3644. copy_data (entry)
  3645.      struct file_entry *entry;
  3646. {
  3647.   register struct relocation_info *reloc;
  3648.   register char *bytes;
  3649.   register int desc;
  3650.  
  3651.   if (trace_files)
  3652.     prline_file_name (entry, stderr);
  3653.  
  3654.   desc = file_open (entry);
  3655.  
  3656.   bytes = (char *) alloca (entry->header.a_data);
  3657.  
  3658.   if (entry->datarel) reloc = entry->datarel;
  3659.   else if (relocatable_output)    /* Will need this again */
  3660.     {
  3661.       read_file_relocation (entry);
  3662.       reloc = entry->datarel;
  3663.     }
  3664.   else
  3665.     {
  3666.       reloc = (struct relocation_info *) alloca (entry->header.a_drsize);
  3667.       lseek (desc, text_offset (entry) + entry->header.a_text
  3668.          + entry->header.a_data + entry->header.a_trsize,
  3669.          0);
  3670.       if (entry->header.a_drsize != read (desc, reloc, entry->header.a_drsize))
  3671.     fatal_with_file ("premature eof in data relocation of ", entry);
  3672.     }
  3673.  
  3674.   lseek (desc, text_offset (entry) + entry->header.a_text, 0);
  3675.   if (entry->header.a_data != read (desc, bytes, entry->header.a_data))
  3676.     fatal_with_file ("premature eof in data section of ", entry);
  3677.  
  3678.   perform_relocation (bytes, entry->data_start_address - entry->header.a_text,
  3679.               entry->header.a_data, reloc, entry->header.a_drsize, entry);
  3680.  
  3681.   mywrite (bytes, 1, entry->header.a_data, outdesc);
  3682. }
  3683.  
  3684. /* Relocate ENTRY's text or data section contents.
  3685.    DATA is the address of the contents, in core.
  3686.    DATA_SIZE is the length of the contents.
  3687.    PC_RELOCATION is the difference between the address of the contents
  3688.      in the output file and its address in the input file.
  3689.    RELOC_INFO is the address of the relocation info, in core.
  3690.    RELOC_SIZE is its length in bytes.  */
  3691. /* This version is about to be severly hacked by Randy.  Hope it
  3692.    works afterwards. */
  3693. void
  3694. perform_relocation (data, pc_relocation, data_size, reloc_info, reloc_size, entry)
  3695.      char *data;
  3696.      struct relocation_info *reloc_info;
  3697.      struct file_entry *entry;
  3698.      int pc_relocation;
  3699.      int data_size;
  3700.      int reloc_size;
  3701. {
  3702.   register struct relocation_info *p = reloc_info;
  3703.   struct relocation_info *end
  3704.     = reloc_info + reloc_size / sizeof (struct relocation_info);
  3705.   int text_relocation = entry->text_start_address;
  3706.   int data_relocation = entry->data_start_address - entry->header.a_text;
  3707.   int bss_relocation
  3708.     = entry->bss_start_address - entry->header.a_text - entry->header.a_data;
  3709.  
  3710.   for (; p < end; p++)
  3711.     {
  3712.       register int relocation = 0;
  3713.       register int addr = RELOC_ADDRESS(p);
  3714.       register unsigned int mask = 0;
  3715.  
  3716.       if (addr >= data_size)
  3717.     fatal_with_file ("relocation address out of range in ", entry);
  3718.  
  3719.       if (RELOC_EXTERN_P(p))
  3720.     {
  3721.       int symindex = RELOC_SYMBOL (p) * sizeof (struct nlist);
  3722.       symbol *sp = ((symbol *)
  3723.             (((struct nlist *)
  3724.               (((char *)entry->symbols) + symindex))
  3725.              ->n_un.n_name));
  3726.  
  3727. #ifdef N_INDR
  3728.       /* Resolve indirection */
  3729.       if ((sp->defined & ~N_EXT) == N_INDR)
  3730.         sp = (symbol *) sp->value;
  3731. #endif
  3732.  
  3733.       if (symindex >= entry->header.a_syms)
  3734.         fatal_with_file ("relocation symbolnum out of range in ", entry);
  3735.  
  3736.       /* If the symbol is undefined, leave it at zero.  */
  3737.       if (! sp->defined)
  3738.         relocation = 0;
  3739.       else
  3740.         relocation = sp->value;
  3741.     }
  3742.       else switch (RELOC_TYPE(p))
  3743.     {
  3744.     case N_TEXT:
  3745.     case N_TEXT | N_EXT:
  3746.       relocation = text_relocation;
  3747.       break;
  3748.  
  3749.     case N_DATA:
  3750.     case N_DATA | N_EXT:
  3751.       /* A word that points to beginning of the the data section
  3752.          initially contains not 0 but rather the "address" of that section
  3753.          in the input file, which is the length of the file's text.  */
  3754.       relocation = data_relocation;
  3755.       break;
  3756.  
  3757.     case N_BSS:
  3758.     case N_BSS | N_EXT:
  3759.       /* Similarly, an input word pointing to the beginning of the bss
  3760.          initially contains the length of text plus data of the file.  */
  3761.       relocation = bss_relocation;
  3762.       break;
  3763.  
  3764.     case N_ABS:
  3765.     case N_ABS | N_EXT:
  3766.       /* Don't know why this code would occur, but apparently it does.  */
  3767.       break;
  3768.  
  3769.     default:
  3770.       fatal_with_file ("nonexternal relocation code invalid in ", entry);
  3771.     }
  3772.  
  3773. #ifdef RELOC_ADD_EXTRA
  3774.       relocation += RELOC_ADD_EXTRA(p);
  3775.       if (relocatable_output)
  3776.     {
  3777.       /* Non-PC relative relocations which are absolute
  3778.          or which have become non-external now have fixed
  3779.          relocations.  Set the ADD_EXTRA of this relocation
  3780.          to be the relocation we have now determined.  */
  3781.       if (! RELOC_PCREL_P (p))
  3782.         {
  3783.           if ((int)p->r_type <= RELOC_32
  3784.           || RELOC_EXTERN_P (p) == 0)
  3785.         RELOC_ADD_EXTRA (p) = relocation;
  3786.         }
  3787.       /* External PC-relative relocations continue to move around;
  3788.          update their relocations by the amount they have moved
  3789.          so far.  */
  3790.       else if (RELOC_EXTERN_P (p))
  3791.         RELOC_ADD_EXTRA (p) -= pc_relocation;
  3792.       continue;
  3793.     }
  3794. #endif
  3795.  
  3796.       if (RELOC_PCREL_P(p))
  3797.     relocation -= pc_relocation;
  3798.  
  3799.       relocation >>= RELOC_VALUE_RIGHTSHIFT(p);
  3800.  
  3801.       /* Unshifted mask for relocation */
  3802.       mask = 1 << RELOC_TARGET_BITSIZE(p) - 1;
  3803.       mask |= mask - 1;
  3804.       relocation &= mask;
  3805.  
  3806.       /* Shift everything up to where it's going to be used */
  3807.       relocation <<= RELOC_TARGET_BITPOS(p);
  3808.       mask <<= RELOC_TARGET_BITPOS(p);
  3809.  
  3810.       switch (RELOC_TARGET_SIZE(p))
  3811.     {
  3812.     case 0:
  3813.       if (RELOC_MEMORY_SUB_P(p))
  3814.         relocation -= mask & *(char *) (data + addr);
  3815.       else if (RELOC_MEMORY_ADD_P(p))
  3816.         relocation += mask & *(char *) (data + addr);
  3817.       *(char *) (data + addr) &= ~mask;
  3818.       *(char *) (data + addr) |= relocation;
  3819.       break;
  3820.  
  3821.     case 1:
  3822.       if (RELOC_MEMORY_SUB_P(p))
  3823.         relocation -= mask & *(short *) (data + addr);
  3824.       else if (RELOC_MEMORY_ADD_P(p))
  3825.         relocation += mask & *(short *) (data + addr);
  3826.       *(short *) (data + addr) &= ~mask;
  3827.       *(short *) (data + addr) |= relocation;
  3828.       break;
  3829.  
  3830.     case 2:
  3831. #ifndef _CROSS_TARGET_ARCH
  3832.       if (RELOC_MEMORY_SUB_P(p))
  3833.         relocation -= mask & *(long *) (data + addr);
  3834.       else if (RELOC_MEMORY_ADD_P(p))
  3835.         relocation += mask & *(long *) (data + addr);
  3836.       *(long *) (data + addr) &= ~mask;
  3837.       *(long *) (data + addr) |= relocation;
  3838. #else
  3839.     /* Handle long word alignment requirements of SPARC architecture */
  3840.     /* WARNING:  This fix makes an assumption on byte ordering */
  3841.     /* Marc Ullman, Stanford University    Nov. 1 1989  */
  3842.       if (RELOC_MEMORY_SUB_P(p)) {
  3843.         relocation -= mask & 
  3844.           ((*(unsigned short *) (data + addr) << 16) |
  3845.         *(unsigned short *) (data + addr + 2));
  3846.       } else if (RELOC_MEMORY_ADD_P(p)) {
  3847.         relocation += mask &
  3848.           ((*(unsigned short *) (data + addr) << 16) |
  3849.         *(unsigned short *) (data + addr + 2));
  3850.       }
  3851.       *(unsigned short *) (data + addr)     &= (~mask >> 16);
  3852.       *(unsigned short *) (data + addr + 2) &= (~mask & 0xffff);
  3853.       *(unsigned short *) (data + addr)     |= (relocation >> 16);
  3854.       *(unsigned short *) (data + addr + 2) |= (relocation & 0xffff);
  3855. #endif
  3856.       break;
  3857.  
  3858.     default:
  3859.       fatal_with_file ("Unimplemented relocation field length in ", entry);
  3860.     }
  3861.     }
  3862. }
  3863.  
  3864. /* For relocatable_output only: write out the relocation,
  3865.    relocating the addresses-to-be-relocated.  */
  3866.  
  3867. void coptxtrel (), copdatrel ();
  3868.  
  3869. void
  3870. write_rel ()
  3871. {
  3872.   register int i;
  3873.   register int count = 0;
  3874.  
  3875.   if (trace_files)
  3876.     fprintf (stderr, "Writing text relocation:\n\n");
  3877.  
  3878.   /* Assign each global symbol a sequence number, giving the order
  3879.      in which `write_syms' will write it.
  3880.      This is so we can store the proper symbolnum fields
  3881.      in relocation entries we write.  */
  3882.  
  3883.   for (i = 0; i < TABSIZE; i++)
  3884.     {
  3885.       symbol *sp;
  3886.       for (sp = symtab[i]; sp; sp = sp->link)
  3887.     if (sp->referenced || sp->defined)
  3888.       {
  3889.         sp->def_count = count++;
  3890.         /* Leave room for the reference required by N_INDR, if
  3891.            necessary.  */
  3892.         if ((sp->defined & ~N_EXT) == N_INDR)
  3893.           count++;
  3894.       }
  3895.     }
  3896.   /* Correct, because if (relocatable_output), we will also be writing
  3897.      whatever indirect blocks we have.  */
  3898.   if (count != defined_global_sym_count
  3899.       + undefined_global_sym_count + global_indirect_count)
  3900.     fatal ("internal error");
  3901.  
  3902.   /* Write out the relocations of all files, remembered from copy_text.  */
  3903.  
  3904.   each_full_file (coptxtrel, 0);
  3905.  
  3906.   if (trace_files)
  3907.     fprintf (stderr, "\nWriting data relocation:\n\n");
  3908.  
  3909.   each_full_file (copdatrel, 0);
  3910.  
  3911.   if (trace_files)
  3912.     fprintf (stderr, "\n");
  3913. }
  3914.  
  3915. void
  3916. coptxtrel (entry)
  3917.      struct file_entry *entry;
  3918. {
  3919.   register struct relocation_info *p, *end;
  3920.   register int reloc = entry->text_start_address;
  3921.  
  3922.   p = entry->textrel;
  3923.   end = (struct relocation_info *) (entry->header.a_trsize + (char *) p);
  3924.   while (p < end)
  3925.     {
  3926.       RELOC_ADDRESS(p) += reloc;
  3927.       if (RELOC_EXTERN_P(p))
  3928.     {
  3929.       register int symindex = RELOC_SYMBOL(p) * sizeof (struct nlist);
  3930.       symbol *symptr = ((symbol *)
  3931.                 (((struct nlist *)
  3932.                   (((char *)entry->symbols) + symindex))
  3933.                  ->n_un.n_name));
  3934.  
  3935.       if (symindex >= entry->header.a_syms)
  3936.         fatal_with_file ("relocation symbolnum out of range in ", entry);
  3937.  
  3938. #ifdef N_INDR
  3939.       /* Resolve indirection.  */
  3940.       if ((symptr->defined & ~N_EXT) == N_INDR)
  3941.         symptr = (symbol *) symptr->value;
  3942. #endif
  3943.  
  3944.       /* If the symbol is now defined, change the external relocation
  3945.          to an internal one.  */
  3946.  
  3947.       if (symptr->defined)
  3948.         {
  3949.           RELOC_EXTERN_P(p) = 0;
  3950.           RELOC_SYMBOL(p) = (symptr->defined & N_TYPE);
  3951. #ifdef RELOC_ADD_EXTRA
  3952.           /* If we aren't going to be adding in the value in
  3953.              memory on the next pass of the loader, then we need
  3954.          to add it in from the relocation entry.  Otherwise
  3955.              the work we did in this pass is lost.  */
  3956.           if (!RELOC_MEMORY_ADD_P(p))
  3957.         RELOC_ADD_EXTRA (p) += symptr->value;
  3958. #endif
  3959.         }
  3960.       else
  3961.         /* Debugger symbols come first, so have to start this
  3962.            after them.  */
  3963.           RELOC_SYMBOL(p) = (symptr->def_count + nsyms
  3964.                  - defined_global_sym_count
  3965.                  - undefined_global_sym_count
  3966.                  - global_indirect_count);
  3967.     }
  3968.       p++;
  3969.     }
  3970.   mywrite (entry->textrel, 1, entry->header.a_trsize, outdesc);
  3971. }
  3972.  
  3973. void
  3974. copdatrel (entry)
  3975.      struct file_entry *entry;
  3976. {
  3977.   register struct relocation_info *p, *end;
  3978.   /* Relocate the address of the relocation.
  3979.      Old address is relative to start of the input file's data section.
  3980.      New address is relative to start of the output file's data section.  */
  3981.   register int reloc = entry->data_start_address - text_size;
  3982.  
  3983.   p = entry->datarel;
  3984.   end = (struct relocation_info *) (entry->header.a_drsize + (char *) p);
  3985.   while (p < end)
  3986.     {
  3987.       RELOC_ADDRESS(p) += reloc;
  3988.       if (RELOC_EXTERN_P(p))
  3989.     {
  3990.       register int symindex = RELOC_SYMBOL(p) * sizeof (struct nlist);
  3991.       symbol *symptr = ((symbol *)
  3992.                 (((struct nlist *)
  3993.                   (((char *)entry->symbols) + symindex))
  3994.                  ->n_un.n_name));
  3995.       int symtype;
  3996.  
  3997.       if (symindex >= entry->header.a_syms)
  3998.         fatal_with_file ("relocation symbolnum out of range in ", entry);
  3999.  
  4000. #ifdef N_INDR
  4001.       /* Resolve indirection.  */
  4002.       if ((symptr->defined & ~N_EXT) == N_INDR)
  4003.         symptr = (symbol *) symptr->value;
  4004. #endif
  4005.  
  4006.        symtype = symptr->defined & N_TYPE;
  4007.  
  4008.       if (force_common_definition
  4009.           || symtype == N_DATA || symtype == N_TEXT || symtype == N_ABS)
  4010.         {
  4011.           RELOC_EXTERN_P(p) = 0;
  4012.           RELOC_SYMBOL(p) = symtype;
  4013.         }
  4014.       else
  4015.         /* Debugger symbols come first, so have to start this
  4016.            after them.  */
  4017.         RELOC_SYMBOL(p)
  4018.           = (((symbol *)
  4019.           (((struct nlist *)
  4020.             (((char *)entry->symbols) + symindex))
  4021.            ->n_un.n_name))
  4022.          ->def_count
  4023.          + nsyms - defined_global_sym_count
  4024.          - undefined_global_sym_count
  4025.          - global_indirect_count);
  4026.     }
  4027.       p++;
  4028.     }
  4029.   mywrite (entry->datarel, 1, entry->header.a_drsize, outdesc);
  4030. }
  4031.  
  4032. void write_file_syms ();
  4033. void write_string_table ();
  4034.  
  4035. /* Offsets and current lengths of symbol and string tables in output file. */
  4036.  
  4037. int symbol_table_offset;
  4038. int symbol_table_len;
  4039.  
  4040. /* Address in output file where string table starts.  */
  4041. int string_table_offset;
  4042.  
  4043. /* Offset within string table
  4044.    where the strings in `strtab_vector' should be written.  */
  4045. int string_table_len;
  4046.  
  4047. /* Total size of string table strings allocated so far,
  4048.    including strings in `strtab_vector'.  */
  4049. int strtab_size;
  4050.  
  4051. /* Vector whose elements are strings to be added to the string table.  */
  4052. char **strtab_vector;
  4053.  
  4054. /* Vector whose elements are the lengths of those strings.  */
  4055. int *strtab_lens;
  4056.  
  4057. /* Index in `strtab_vector' at which the next string will be stored.  */
  4058. int strtab_index;
  4059.  
  4060. /* Add the string NAME to the output file string table.
  4061.    Record it in `strtab_vector' to be output later.
  4062.    Return the index within the string table that this string will have.  */
  4063.  
  4064. int
  4065. assign_string_table_index (name)
  4066.      char *name;
  4067. {
  4068.   register int index = strtab_size;
  4069.   register int len = strlen (name) + 1;
  4070.  
  4071.   strtab_size += len;
  4072.   strtab_vector[strtab_index] = name;
  4073.   strtab_lens[strtab_index++] = len;
  4074.  
  4075.   return index;
  4076. }
  4077.  
  4078. FILE *outstream = (FILE *) 0;
  4079.  
  4080. /* Write the contents of `strtab_vector' into the string table.
  4081.    This is done once for each file's local&debugger symbols
  4082.    and once for the global symbols.  */
  4083.  
  4084. void
  4085. write_string_table ()
  4086. {
  4087.   register int i;
  4088.  
  4089.   lseek (outdesc, string_table_offset + string_table_len, 0);
  4090.  
  4091.   if (!outstream)
  4092.     outstream = fdopen (outdesc, "w");
  4093.  
  4094.   for (i = 0; i < strtab_index; i++)
  4095.     {
  4096.       fwrite (strtab_vector[i], 1, strtab_lens[i], outstream);
  4097.       string_table_len += strtab_lens[i];
  4098.     }
  4099.  
  4100.   fflush (outstream);
  4101.  
  4102.   /* Report I/O error such as disk full.  */
  4103.   if (ferror (outstream))
  4104.     perror_name (output_filename);
  4105. }
  4106.  
  4107. /* Write the symbol table and string table of the output file.  */
  4108.  
  4109. void
  4110. write_syms ()
  4111. {
  4112.   /* Number of symbols written so far.  */
  4113.   int syms_written = 0;
  4114.   register int i;
  4115.   register symbol *sp;
  4116.  
  4117.   /* Buffer big enough for all the global symbols.  One
  4118.      extra struct for each indirect symbol to hold the extra reference
  4119.      following. */
  4120.   struct nlist *buf
  4121.     = (struct nlist *) alloca ((defined_global_sym_count
  4122.                 + undefined_global_sym_count
  4123.                 + global_indirect_count)
  4124.                    * sizeof (struct nlist));
  4125.   /* Pointer for storing into BUF.  */
  4126.   register struct nlist *bufp = buf;
  4127.  
  4128.   /* Size of string table includes the bytes that store the size.  */
  4129.   strtab_size = sizeof strtab_size;
  4130.  
  4131.   symbol_table_offset = N_SYMOFF (outheader);
  4132.   symbol_table_len = 0;
  4133.   string_table_offset = N_STROFF (outheader);
  4134.   string_table_len = strtab_size;
  4135.  
  4136.   if (strip_symbols == STRIP_ALL)
  4137.     return;
  4138.  
  4139.   /* Write the local symbols defined by the various files.  */
  4140.  
  4141.   each_file (write_file_syms, &syms_written);
  4142.   file_close ();
  4143.  
  4144.   /* Now write out the global symbols.  */
  4145.  
  4146.   /* Allocate two vectors that record the data to generate the string
  4147.      table from the global symbols written so far.  This must include
  4148.      extra space for the references following indirect outputs. */
  4149.  
  4150.   strtab_vector = (char **) alloca ((num_hash_tab_syms
  4151.                      + global_indirect_count) * sizeof (char *));
  4152.   strtab_lens = (int *) alloca ((num_hash_tab_syms
  4153.                  + global_indirect_count) * sizeof (int));
  4154.   strtab_index = 0;
  4155.  
  4156.   /* Scan the symbol hash table, bucket by bucket.  */
  4157.  
  4158.   for (i = 0; i < TABSIZE; i++)
  4159.     for (sp = symtab[i]; sp; sp = sp->link)
  4160.       {
  4161.     struct nlist nl;
  4162.  
  4163.     nl.n_other = 0;
  4164.     nl.n_desc = 0;
  4165.  
  4166.     /* Compute a `struct nlist' for the symbol.  */
  4167.  
  4168.     if (sp->defined || sp->referenced)
  4169.       {
  4170.         /* common condition needs to be before undefined condition */
  4171.         /* because unallocated commons are set undefined in */
  4172.         /* digest_symbols */
  4173.         if (sp->defined > 1) /* defined with known type */
  4174.           {
  4175.         /* If the target of an indirect symbol has been
  4176.            defined and we are outputting an executable,
  4177.            resolve the indirection; it's no longer needed */
  4178.         if (!relocatable_output
  4179.             && ((sp->defined & N_TYPE) == N_INDR)
  4180.             && (((symbol *) sp->value)->defined > 1))
  4181.           {
  4182.             symbol *newsp = (symbol *) sp->value;
  4183.             nl.n_type = newsp->defined;
  4184.             nl.n_value = newsp->value;
  4185.           }
  4186.         else
  4187.           {
  4188.             nl.n_type = sp->defined;
  4189.             if (sp->defined != (N_INDR | N_EXT))
  4190.               nl.n_value = sp->value;
  4191.             else
  4192.               nl.n_value = 0;
  4193.           }
  4194.           }
  4195.         else if (sp->max_common_size) /* defined as common but not allocated. */
  4196.           {
  4197.         /* happens only with -r and not -d */
  4198.         /* write out a common definition */
  4199.         nl.n_type = N_UNDF | N_EXT;
  4200.         nl.n_value = sp->max_common_size;
  4201.           }
  4202.         else if (!sp->defined)          /* undefined -- legit only if -r */
  4203.           {
  4204.         nl.n_type = N_UNDF | N_EXT;
  4205.         nl.n_value = 0;
  4206.           }
  4207.         else
  4208.           fatal ("internal error: %s defined in mysterious way", sp->name);
  4209.  
  4210.         /* Allocate string table space for the symbol name.  */
  4211.  
  4212.         nl.n_un.n_strx = assign_string_table_index (sp->name);
  4213.  
  4214.         /* Output to the buffer and count it.  */
  4215.  
  4216.         *bufp++ = nl;
  4217.         syms_written++;
  4218.         if (nl.n_type == (N_INDR | N_EXT))
  4219.           {
  4220.         struct nlist xtra_ref;
  4221.         xtra_ref.n_type == N_EXT | N_UNDF;
  4222.         xtra_ref.n_un.n_strx
  4223.           = assign_string_table_index (((symbol *) sp->value)->name);
  4224.         xtra_ref.n_other = 0;
  4225.         xtra_ref.n_desc = 0;
  4226.         xtra_ref.n_value = 0;
  4227.         *bufp++ = xtra_ref;
  4228.         syms_written++;
  4229.           }
  4230.       }
  4231.       }
  4232.  
  4233.   /* Output the buffer full of `struct nlist's.  */
  4234.  
  4235.   lseek (outdesc, symbol_table_offset + symbol_table_len, 0);
  4236.   mywrite (buf, sizeof (struct nlist), bufp - buf, outdesc);
  4237.   symbol_table_len += sizeof (struct nlist) * (bufp - buf);
  4238.  
  4239.   if (syms_written != nsyms)
  4240.     fatal ("internal error: wrong number of symbols written into output file", 0);
  4241.  
  4242.   if (symbol_table_offset + symbol_table_len != string_table_offset)
  4243.     fatal ("internal error: inconsistent symbol table length", 0);
  4244.  
  4245.   /* Now the total string table size is known, so write it.
  4246.      We are already positioned at the right place in the file.  */
  4247.  
  4248.   mywrite (&strtab_size, sizeof (int), 1, outdesc);  /* we're at right place */
  4249.  
  4250.   /* Write the strings for the global symbols.  */
  4251.  
  4252.   write_string_table ();
  4253. }
  4254.  
  4255. /* Write the local and debugger symbols of file ENTRY.
  4256.    Increment *SYMS_WRITTEN_ADDR for each symbol that is written.  */
  4257.  
  4258. /* Note that we do not combine identical names of local symbols.
  4259.    dbx or gdb would be confused if we did that.  */
  4260.  
  4261. void
  4262. write_file_syms (entry, syms_written_addr)
  4263.      struct file_entry *entry;
  4264.      int *syms_written_addr;
  4265. {
  4266.   register struct nlist *p = entry->symbols;
  4267.   register struct nlist *end = p + entry->header.a_syms / sizeof (struct nlist);
  4268.  
  4269.   /* Buffer to accumulate all the syms before writing them.
  4270.      It has one extra slot for the local symbol we generate here.  */
  4271.   struct nlist *buf
  4272.     = (struct nlist *) alloca (entry->header.a_syms + sizeof (struct nlist));
  4273.   register struct nlist *bufp = buf;
  4274.  
  4275.   /* Upper bound on number of syms to be written here.  */
  4276.   int max_syms = (entry->header.a_syms / sizeof (struct nlist)) + 1;
  4277.  
  4278.   /* Make tables that record, for each symbol, its name and its name's length.
  4279.      The elements are filled in by `assign_string_table_index'.  */
  4280.  
  4281.   strtab_vector = (char **) alloca (max_syms * sizeof (char *));
  4282.   strtab_lens = (int *) alloca (max_syms * sizeof (int));
  4283.   strtab_index = 0;
  4284.  
  4285.   /* Generate a local symbol for the start of this file's text.  */
  4286.  
  4287.   if (discard_locals != DISCARD_ALL)
  4288.     {
  4289.       struct nlist nl;
  4290.  
  4291.       nl.n_type = N_TEXT;
  4292.       nl.n_un.n_strx = assign_string_table_index (entry->local_sym_name);
  4293.       nl.n_value = entry->text_start_address;
  4294.       nl.n_desc = 0;
  4295.       nl.n_other = 0;
  4296.       *bufp++ = nl;
  4297.       (*syms_written_addr)++;
  4298.       entry->local_syms_offset = *syms_written_addr * sizeof (struct nlist);
  4299.     }
  4300.  
  4301.   /* Read the file's string table.  */
  4302.  
  4303.   entry->strings = (char *) alloca (entry->string_size);
  4304.   read_entry_strings (file_open (entry), entry);
  4305.  
  4306.   for (; p < end; p++)
  4307.     {
  4308.       register int type = p->n_type;
  4309.       register int write = 0;
  4310.  
  4311.       /* WRITE gets 1 for a non-global symbol that should be written.  */
  4312.  
  4313.  
  4314.       if (SET_ELEMENT_P (type))    /* This occurs even if global.  These */
  4315.                 /* types of symbols are never written */
  4316.                 /* globally, though they are stored */
  4317.                 /* globally.  */
  4318.         write = relocatable_output;
  4319.       else if (!(type & (N_STAB | N_EXT)))
  4320.         /* ordinary local symbol */
  4321.     write = ((discard_locals != DISCARD_ALL)
  4322.          && !(discard_locals == DISCARD_L &&
  4323.               (p->n_un.n_strx + entry->strings)[0] == LPREFIX)
  4324.          && type != N_WARNING);
  4325.       else if (!(type & N_EXT))
  4326.     /* debugger symbol */
  4327.         write = (strip_symbols == STRIP_NONE);
  4328.  
  4329.       if (write)
  4330.     {
  4331.       /* If this symbol has a name,
  4332.          allocate space for it in the output string table.  */
  4333.  
  4334.       if (p->n_un.n_strx)
  4335.         p->n_un.n_strx = assign_string_table_index (p->n_un.n_strx
  4336.                             + entry->strings);
  4337.  
  4338.       /* Output this symbol to the buffer and count it.  */
  4339.  
  4340.       *bufp++ = *p;
  4341.       (*syms_written_addr)++;
  4342.     }
  4343.     }
  4344.  
  4345.   /* All the symbols are now in BUF; write them.  */
  4346.  
  4347.   lseek (outdesc, symbol_table_offset + symbol_table_len, 0);
  4348.   mywrite (buf, sizeof (struct nlist), bufp - buf, outdesc);
  4349.   symbol_table_len += sizeof (struct nlist) * (bufp - buf);
  4350.  
  4351.   /* Write the string-table data for the symbols just written,
  4352.      using the data in vectors `strtab_vector' and `strtab_lens'.  */
  4353.  
  4354.   write_string_table ();
  4355.   entry->strings = 0;        /* Since it will dissapear anyway.  */
  4356. }
  4357.  
  4358. /* Copy any GDB symbol segments from the input files to the output file.
  4359.    The contents of the symbol segment is copied without change
  4360.    except that we store some information into the beginning of it.  */
  4361.  
  4362. void write_file_symseg ();
  4363.  
  4364. void
  4365. write_symsegs ()
  4366. {
  4367.   each_file (write_file_symseg, 0);
  4368. }
  4369.  
  4370. void
  4371. write_file_symseg (entry)
  4372.      struct file_entry *entry;
  4373. {
  4374.   char buffer[4096];
  4375.   struct symbol_root root;
  4376.   int indesc;
  4377.   int len;
  4378.  
  4379.   if (entry->symseg_offset == 0)
  4380.     return;
  4381.  
  4382.   /* This entry has a symbol segment.  Read the root of the segment.  */
  4383.  
  4384.   indesc = file_open (entry);
  4385.   lseek (indesc, entry->symseg_offset + entry->starting_offset, 0);
  4386.   if (sizeof root != read (indesc, &root, sizeof root))
  4387.     fatal_with_file ("premature end of file in symbol segment of ", entry);
  4388.  
  4389.   /* Store some relocation info into the root.  */
  4390.  
  4391.   root.ldsymoff = entry->local_syms_offset;
  4392.   root.textrel = entry->text_start_address;
  4393.   root.datarel = entry->data_start_address - entry->header.a_text;
  4394.   root.bssrel = entry->bss_start_address
  4395.     - entry->header.a_text - entry->header.a_data;
  4396.   root.databeg = entry->data_start_address - root.datarel;
  4397.   root.bssbeg = entry->bss_start_address - root.bssrel;
  4398.  
  4399.   /* Write the modified root into the output file.  */
  4400.  
  4401.   mywrite (&root, sizeof root, 1, outdesc);
  4402.  
  4403.   /* Copy the rest of the symbol segment unchanged.  */
  4404.  
  4405.   if (entry->superfile)
  4406.     {
  4407.       /* Library member: number of bytes to copy is determined
  4408.      from the member's total size.  */
  4409.  
  4410.       int total = entry->total_size - entry->symseg_offset - sizeof root;
  4411.  
  4412.       while (total > 0)
  4413.     {
  4414.       len = read (indesc, buffer, min (sizeof buffer, total));
  4415.  
  4416.       if (len != min (sizeof buffer, total))
  4417.         fatal_with_file ("premature end of file in symbol segment of ", entry);
  4418.       total -= len;
  4419.       mywrite (buffer, len, 1, outdesc);
  4420.     }
  4421.     }
  4422.   else
  4423.     {
  4424.       /* A separate file: copy until end of file.  */
  4425.  
  4426.       while (len = read (indesc, buffer, sizeof buffer))
  4427.     {
  4428.       mywrite (buffer, len, 1, outdesc);
  4429.       if (len < sizeof buffer)
  4430.         break;
  4431.     }
  4432.     }
  4433.  
  4434.   file_close ();
  4435. }
  4436.  
  4437. /* Create the symbol table entries for `etext', `edata' and `end'.  */
  4438.  
  4439. void
  4440. symtab_init ()
  4441. {
  4442. #ifndef nounderscore
  4443.   edata_symbol = getsym ("_edata");
  4444.   etext_symbol = getsym ("_etext");
  4445.   end_symbol = getsym ("_end");
  4446. #else
  4447.   edata_symbol = getsym ("edata");
  4448.   etext_symbol = getsym ("etext");
  4449.   end_symbol = getsym ("end");
  4450. #endif
  4451.  
  4452. #ifdef sun
  4453.   {
  4454.     symbol *dynamic_symbol = getsym ("__DYNAMIC");
  4455.     dynamic_symbol->defined = N_ABS | N_EXT;
  4456.     dynamic_symbol->referenced = 1;
  4457.     dynamic_symbol->value = 0;
  4458.   }
  4459. #endif
  4460.  
  4461. #ifdef sequent
  4462.   {
  4463.     symbol *_387_flt_symbol = getsym ("_387_flt");
  4464.     _387_flt_symbol->defined = N_ABS | N_EXT;
  4465.     _387_flt_symbol->referenced = 1;
  4466.     _387_flt_symbol->value = 0;
  4467.   }
  4468. #endif
  4469.  
  4470.   edata_symbol->defined = N_DATA | N_EXT;
  4471.   etext_symbol->defined = N_TEXT | N_EXT;
  4472.   end_symbol->defined = N_BSS | N_EXT;
  4473.  
  4474.   edata_symbol->referenced = 1;
  4475.   etext_symbol->referenced = 1;
  4476.   end_symbol->referenced = 1;
  4477. }
  4478.  
  4479. /* Compute the hash code for symbol name KEY.  */
  4480.  
  4481. int
  4482. hash_string (key)
  4483.      char *key;
  4484. {
  4485.   register char *cp;
  4486.   register int k;
  4487.  
  4488.   cp = key;
  4489.   k = 0;
  4490.   while (*cp)
  4491.     k = (((k << 1) + (k >> 14)) ^ (*cp++)) & 0x3fff;
  4492.  
  4493.   return k;
  4494. }
  4495.  
  4496. /* Get the symbol table entry for the global symbol named KEY.
  4497.    Create one if there is none.  */
  4498.  
  4499. symbol *
  4500. getsym (key)
  4501.      char *key;
  4502. {
  4503.   register int hashval;
  4504.   register symbol *bp;
  4505.  
  4506.   /* Determine the proper bucket.  */
  4507.  
  4508.   hashval = hash_string (key) % TABSIZE;
  4509.  
  4510.   /* Search the bucket.  */
  4511.  
  4512.   for (bp = symtab[hashval]; bp; bp = bp->link)
  4513.     if (! strcmp (key, bp->name))
  4514.       return bp;
  4515.  
  4516.   /* Nothing was found; create a new symbol table entry.  */
  4517.  
  4518.   bp = (symbol *) xmalloc (sizeof (symbol));
  4519.   bp->refs = 0;
  4520.   bp->name = (char *) xmalloc (strlen (key) + 1);
  4521.   strcpy (bp->name, key);
  4522.   bp->defined = 0;
  4523.   bp->referenced = 0;
  4524.   bp->trace = 0;
  4525.   bp->value = 0;
  4526.   bp->max_common_size = 0;
  4527.   bp->warning = 0;
  4528.   bp->undef_refs = 0;
  4529.   bp->multiply_defined = 0;
  4530.  
  4531.   /* Add the entry to the bucket.  */
  4532.  
  4533.   bp->link = symtab[hashval];
  4534.   symtab[hashval] = bp;
  4535.  
  4536.   ++num_hash_tab_syms;
  4537.  
  4538.   return bp;
  4539. }
  4540.  
  4541. /* Like `getsym' but return 0 if the symbol is not already known.  */
  4542.  
  4543. symbol *
  4544. getsym_soft (key)
  4545.      char *key;
  4546. {
  4547.   register int hashval;
  4548.   register symbol *bp;
  4549.  
  4550.   /* Determine which bucket.  */
  4551.  
  4552.   hashval = hash_string (key) % TABSIZE;
  4553.  
  4554.   /* Search the bucket.  */
  4555.  
  4556.   for (bp = symtab[hashval]; bp; bp = bp->link)
  4557.     if (! strcmp (key, bp->name))
  4558.       return bp;
  4559.  
  4560.   return 0;
  4561. }
  4562.  
  4563. /* Report a fatal error.
  4564.    STRING is a printf format string and ARG is one arg for it.  */
  4565.  
  4566. void
  4567. fatal (string, arg)
  4568.      char *string, *arg;
  4569. {
  4570.   fprintf (stderr, "ld: ");
  4571.   fprintf (stderr, string, arg);
  4572.   fprintf (stderr, "\n");
  4573.   exit (1);
  4574. }
  4575.  
  4576. /* Report a fatal error.  The error message is STRING
  4577.    followed by the filename of ENTRY.  */
  4578.  
  4579. void
  4580. fatal_with_file (string, entry)
  4581.      char *string;
  4582.      struct file_entry *entry;
  4583. {
  4584.   fprintf (stderr, "ld: ");
  4585.   fprintf (stderr, string);
  4586.   print_file_name (entry, stderr);
  4587.   fprintf (stderr, "\n");
  4588.   exit (1);
  4589. }
  4590.  
  4591. /* Report a fatal error using the message for the last failed system call,
  4592.    followed by the string NAME.  */
  4593.  
  4594. void
  4595. perror_name (name)
  4596.      char *name;
  4597. {
  4598.   extern int errno, sys_nerr;
  4599.   extern char *sys_errlist[];
  4600.   char *s;
  4601.  
  4602.   if (errno < sys_nerr)
  4603.     s = concat ("", sys_errlist[errno], " for %s");
  4604.   else
  4605.     s = "cannot open %s";
  4606.   fatal (s, name);
  4607. }
  4608.  
  4609. /* Report a fatal error using the message for the last failed system call,
  4610.    followed by the name of file ENTRY.  */
  4611.  
  4612. void
  4613. perror_file (entry)
  4614.      struct file_entry *entry;
  4615. {
  4616.   extern int errno, sys_nerr;
  4617.   extern char *sys_errlist[];
  4618.   char *s;
  4619.  
  4620.   if (errno < sys_nerr)
  4621.     s = concat ("", sys_errlist[errno], " for ");
  4622.   else
  4623.     s = "cannot open ";
  4624.   fatal_with_file (s, entry);
  4625. }
  4626.  
  4627. /* Report a nonfatal error.
  4628.    STRING is a format for printf, and ARG1 ... ARG3 are args for it.  */
  4629.  
  4630. void
  4631. error (string, arg1, arg2, arg3)
  4632.      char *string, *arg1, *arg2, *arg3;
  4633. {
  4634.   fprintf (stderr, "%s: ", progname);
  4635.   fprintf (stderr, string, arg1, arg2, arg3);
  4636.   fprintf (stderr, "\n");
  4637. }
  4638.  
  4639.  
  4640. /* Output COUNT*ELTSIZE bytes of data at BUF
  4641.    to the descriptor DESC.  */
  4642.  
  4643. void
  4644. mywrite (buf, count, eltsize, desc)
  4645.      char *buf;
  4646.      int count;
  4647.      int eltsize;
  4648.      int desc;
  4649. {
  4650.   register int val;
  4651.   register int bytes = count * eltsize;
  4652.  
  4653.   while (bytes > 0)
  4654.     {
  4655.       val = write (desc, buf, bytes);
  4656.       if (val <= 0)
  4657.     perror_name (output_filename);
  4658.       buf += val;
  4659.       bytes -= val;
  4660.     }
  4661. }
  4662.  
  4663. /* Output PADDING zero-bytes to descriptor OUTDESC.
  4664.    PADDING may be negative; in that case, do nothing.  */
  4665.  
  4666. void
  4667. padfile (padding, outdesc)
  4668.      int padding;
  4669.      int outdesc;
  4670. {
  4671.   register char *buf;
  4672.   if (padding <= 0)
  4673.     return;
  4674.  
  4675.   buf = (char *) alloca (padding);
  4676.   bzero (buf, padding);
  4677.   mywrite (buf, padding, 1, outdesc);
  4678. }
  4679.  
  4680. /* Return a newly-allocated string
  4681.    whose contents concatenate the strings S1, S2, S3.  */
  4682.  
  4683. char *
  4684. concat (s1, s2, s3)
  4685.      char *s1, *s2, *s3;
  4686. {
  4687.   register int len1 = strlen (s1), len2 = strlen (s2), len3 = strlen (s3);
  4688.   register char *result = (char *) xmalloc (len1 + len2 + len3 + 1);
  4689.  
  4690.   strcpy (result, s1);
  4691.   strcpy (result + len1, s2);
  4692.   strcpy (result + len1 + len2, s3);
  4693.   result[len1 + len2 + len3] = 0;
  4694.  
  4695.   return result;
  4696. }
  4697.  
  4698. /* Parse the string ARG using scanf format FORMAT, and return the result.
  4699.    If it does not parse, report fatal error
  4700.    generating the error message using format string ERROR and ARG as arg.  */
  4701.  
  4702. int
  4703. parse (arg, format, error)
  4704.      char *arg, *format;
  4705. {
  4706.   int x;
  4707.   if (1 != sscanf (arg, format, &x))
  4708.     fatal (error, arg);
  4709.   return x;
  4710. }
  4711.  
  4712. /* Like malloc but get fatal error if memory is exhausted.  */
  4713.  
  4714. int
  4715. xmalloc (size)
  4716.      int size;
  4717. {
  4718.   register int result = malloc (size);
  4719.   if (!result)
  4720.     fatal ("virtual memory exhausted", 0);
  4721.   return result;
  4722. }
  4723.  
  4724. /* Like realloc but get fatal error if memory is exhausted.  */
  4725.  
  4726. int
  4727. xrealloc (ptr, size)
  4728.      char *ptr;
  4729.      int size;
  4730. {
  4731.   register int result = realloc (ptr, size);
  4732.   if (!result)
  4733.     fatal ("virtual memory exhausted", 0);
  4734.   return result;
  4735. }
  4736.  
  4737. #ifdef USG
  4738.  
  4739. void
  4740. bzero (p, n)
  4741.      char *p;
  4742. {
  4743.   memset (p, 0, n);
  4744. }
  4745.  
  4746. void
  4747. bcopy (from, to, n)
  4748.      char *from, *to;
  4749. {
  4750.   memcpy (to, from, n);
  4751. }
  4752.  
  4753. getpagesize ()
  4754. {
  4755.   return (4096);
  4756. }
  4757.  
  4758. #endif
  4759.  
  4760. #if TARGET == SUN4
  4761.  
  4762. /* Don't use local pagesize to build for Sparc.  */
  4763.  
  4764. getpagesize ()
  4765. {
  4766.   return (8192);
  4767. }
  4768. #endif
  4769.